Please consider the following code:
Connection[] MainConnection = new Connection[7]; String[] RemoteIPAddress = new String[7]; /* RemoteIPAddress[0] = "aa.aa.a.aaa"; RemoteIPAddress[1] = "bb.bb.b.bbb"; RemoteIPAddress[2] = "cc.cc.c.ccc"; RemoteIPAddress[3] = "dd.dd.d.ddd"; RemoteIPAddress[4] = "ee.ee.e.eee"; RemoteIPAddress[5] = "ff.ff.f.fff"; RemoteIPAddress[6] = "gg.gg.g.ggg";*/ while(i<7) { try { Connection connRemote = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test",MainUser,MainPass); String maindbsql = "SELECT IP_vch FROM Maindb.TableIPStatus WHERE IPStatus = 1"; Map<String, Connection> connections = new HashMap<>(); Statement stmt = connRemote.createStatement(); Resultset rs = stmt.executeQuery(maindbsql); while(rs.next()){ final String ipAddress = rs.getString("IP_vch"); System.out.println("The value of ipAddress is:"+ipAddress); connection.put(ipAddress,DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass)); }// END Of WHILE (rs.next()) //MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[i] + ":3306/test",RemoteUser,RemotePass); String QueryString = "SELECT Query goes here"; SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); SelectResultsRS = SelectResultsStmt.executeQuery(QueryString); while(SelectResultRS.next()) { String QueryStringInsertFromRemote = "INSERT INTO MY TABLE" "(" +"IP_vch" +" ) " +"VALUES" +"(" + " '" + RemoteIPAddress[i] + "' " +")"; } }catch(SQLException e){ e.printStackTrace(); } i++; } END OF ORIGINAL WHILE LOOP while (i < 7)
As seen in some part of my commented code, I was using MainConnection and harcoding the IP addresses before. But since, my query is getting all the eligible(IPStatus = 1) IP addresses,I don't need to hardcode them anymore
and hence I am using Map interface to store the eligible IP addresses as shown above.
Problem I am encountering are as follows:
1) When I was using MainConnection and hardcoded IP addresses, I was using something like the following to make my resultset updatable:
SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
But, considering the fact that I am no longer using the Mainconnection thing, how should I do the same thing with the Resultset now using the HashMap?
2) Similarly, if we go further down the code, in the QueryStringInsertFromRemote , I am inserting the value of the IP address for which the connection has been established at that very moment. Please let me know how can I
overcome with these problem as I am using HashMap here?
3) 1) Considering the fact that I am not hardcoding IP addresses, do I need the while loop now?
Thanks