Java 使用映射接口对象执行查询

Java 使用映射接口对象执行查询,java,jdbc,Java,Jdbc,在进行任何更改之前,我随身携带以下代码: Connection connRemote = DriverManager.getConnection("jdbc:mysql://11.11.1.111:3306/test",MainUser,MainPass); String maindbsql = null; maindbsql = "SELECT IP_vch FROM table1 WHERE table_dt = 1"; // Say for example I have followin

在进行任何更改之前,我随身携带以下代码:

Connection connRemote = DriverManager.getConnection("jdbc:mysql://11.11.1.111:3306/test",MainUser,MainPass);
String maindbsql = null;
maindbsql = "SELECT IP_vch FROM table1 WHERE table_dt = 1"; 
// Say for example I have following values for IP_vch 11.11.11.111,22.22.22.222,33.33.33.333
Map<String,Connection> connections = new HashMap<>();
DeviceStmt = connRemote.createStatement();
DeviceRS = DeviceStmt.executeQuery(maindbsql);
 while(DeviceRS.next()){
 try{ 
  final String ip_address = DeviceRS.getString("IP_vch");
  System.out.println("Value of IP_vch Field:"+ip_address);
  connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
  System.out.println("Connection to "+ip_address+" is successfull");
 }
 catch(SQLException ex1){
  System.out.println(" Inner While Loop Stack Trace Below:");
  ex1.printStackTrace();
 }
}//END Of DeviceRS.next() while loop 

但是,如果我们考虑下面的代码(与上面略有不同):

我想把上面两行放在上面代码的下一行下面:

connections.put(ip_地址,DriverManager.getConnection(“jdbc:mysql://“+ip_地址+”:3306/test”,RemoteUser,RemotePass))


请告知连接有什么问题。获取(ip地址)。执行(…)
?(当然,使用适当的空检查)我没有尝试过。让我试试。Thanks@JimGarrison您能告诉我在这种情况下如何创建
createStatement()
,因为我们直接编写
executeQuery(loadchecksql)。如果我没有错的话,我只是替换了以下两行
LoadCheckStmt=connRemote.createStatement();LoadCheckRS=LoadCheckStmt.executeQuery(loadchecksql)
连接。获取(ip地址)。执行(…)
和获取错误。这就是我的意思。我的暗示只是想让你找到正确的方向。谢谢。我让它工作了。
loadchecksql = "SELECT COUNT(*) FROM table2";
Connection connRemote = DriverManager.getConnection("jdbc:mysql://11.11.1.111:3306/test",MainUser,MainPass);
String maindbsql = null;
maindbsql = "SELECT IP_vch FROM table1 WHERE table_dt = 1"; 
loadchecksql = "SELECT COUNT(*) FROM table2";// this table2 exists only in the IP address that are in `IP_vch` field
// Say for example I have following values for IP_vch 11.11.11.111,22.22.22.222,33.33.33.333
Map<String,Connection> connections = new HashMap<>();
DeviceStmt = connRemote.createStatement();
DeviceRS = DeviceStmt.executeQuery(maindbsql);
 while(DeviceRS.next()){
 try{ 
  final String ip_address = DeviceRS.getString("IP_vch");
  System.out.println("Value of IP_vch Field:"+ip_address);
  connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
  System.out.println("Connection to "+ip_address+" is successfull");
 }
 catch(SQLException ex1){
  System.out.println(" Inner While Loop Stack Trace Below:");
  ex1.printStackTrace();
 }
}//END Of DeviceRS.next() while loop 
LoadCheckStmt = connRemote.createStatement();
LoadCheckRS = LoadCheckStmt.executeQuery(loadchecksql);