Here is what I have right now where, I am making multiple JDBC connections based on the IP address I have.

Java Code:
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())
What I want to have :
Say for example I am making connections to following IP's : 11.11.1.111 and 22.22.2.222

I want to execute an additional query on 11.11.1.111 just after connection is established after the following line in the above code:
connections.put(ip_address,DriverManager.getConnec tion("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));


Since, I can't use the same resultset(DeviceRS) which I have already used , I am wondering how can I run a different query.

I want to do something like the following:

Java Code:
 DeviceStmttwo = connRemote.createStatement();
                    DeviceRStwo = DeviceStmttwo.executeQuery(maindbsqlnew);
(Please note that maindbsqlnew query is different than maindbsql)

below the following code I mentioned in the above code:

Java Code:
		DeviceStmt = connRemote.createStatement();
                    DeviceRS = DeviceStmt.executeQuery(maindbsql);

Should I go for different while(DeviceRStwo.next()) loop option just before the existing while(DeviceRS.next()) loop. This is will result in calling DriverManager.getConnection two times in two
different while loops which seems to me an overhead.



Someone suggested me to have a method and then call the method two different times as follows:


Java Code:
Map<String, Connection> connections = new HashMap<>();//declare outside

 public void addConnections(Connection connRemote, String query, Map<String,Connection> connections) {
                DeviceStmt = connRemote.createStatement();
                DeviceRS = DeviceStmt.executeQuery(query);

                while (DeviceRS.next()) {
                    try {
                        String ip_address = DeviceRS.getString("IP_vch");
                        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.err.println(ex1);
                    }
                }//END Of while(DeviceRS.next())
            }
			
			
addConnections(connRemote, maindbsql,connections);
addConnections(connRemote, maindbsqlnew, connections);
But the problem with the above is that 1) The connections is getting established two times each time the method is called.
2) I have a different query for maindbsqlnew , so the line String ip_address = DeviceRS.getString("IP_vch"); won't work for me.

Is there an efficient way of achieving what I am looking for without making connections two times?