对不同的结果集使用两个while循环或单循环

时间:2014-05-22 19:25:50

标签: java jdbc

以下是我现在所处的内容,我根据我的IP地址建立了多个JDBC连接。

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));

        if (connections.isEmpty()) {
            System.err.println("Not successfull");
        } else {
            System.out.println("Connection to"+ip_address+"is successfull");
        }
    } catch(SQLException ex1) {
        System.out.println("While Loop Stack Trace Below:");
        ex1.printStackTrace();
    }
}//END Of while(DeviceRS.next())

我想要的是: 比方说,我正在建立与以下IP的连接:11.11.1.111和22.22.2.222

我想在上面代码中的以下行之后建立连接后立即在11.11.1.111上执行其他查询: connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));

因为,我不能使用我已经使用的相同结果集(DeviceRS),我想知道如何运行不同的查询。

我想做以下事情:

DeviceStmttwo = connRemote.createStatement();
DeviceRStwo = DeviceStmttwo.executeQuery(maindbsqlnew);

下面我在上面的代码中提到的代码:

DeviceStmt = connRemote.createStatement();
DeviceRS = DeviceStmt.executeQuery(maindbsql);

我应该在现有的while(DeviceRS.next())循环之前选择不同的(DeviceRStwo.next())选项并再次建立连接,这似乎是我的开销。

0 个答案:

没有答案