java - using map interface object to execute a query -
i have following code me before making changes:
connection connremote = drivermanager.getconnection("jdbc:mysql://11.11.1.111:3306/test",mainuser,mainpass); string maindbsql = null; maindbsql = "select ip_vch table1 table_dt = 1"; // example have following values 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 "+ip_address+" successfull"); } catch(sqlexception ex1){ system.out.println(" inner while loop stack trace below:"); ex1.printstacktrace(); } }//end of devicers.next() while loop
now, want put check using sql query follows:
loadchecksql = "select count(*) table2";
however, if consider following code(slightly different above):
connection connremote = drivermanager.getconnection("jdbc:mysql://11.11.1.111:3306/test",mainuser,mainpass); string maindbsql = null; maindbsql = "select ip_vch table1 table_dt = 1"; loadchecksql = "select count(*) table2";// table2 exists in ip address in `ip_vch` field // example have following values 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 "+ip_address+" successfull"); } catch(sqlexception ex1){ system.out.println(" inner while loop stack trace below:"); ex1.printstacktrace(); } }//end of devicers.next() while loop
but connections
object of map interface , hence wondering how can make loadchecksql
work since have following can't use connections
below in place of connremote
:
loadcheckstmt = connremote.createstatement(); loadcheckrs = loadcheckstmt.executequery(loadchecksql);
i thinking put above 2 lines below following line in above code:
connections.put(ip_address,drivermanager.getconnection("jdbc:mysql://" + ip_address + ":3306/test",remoteuser,remotepass));
please advise
Comments
Post a Comment