I am naming database located at xx.xx.x.xxx as Maindb for our discussion purpose. I have a table in the Maindb and column with name "IPStatus".while (i < 7){ try{ 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"; MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[i] + ":3306/test",RemoteUser,RemotePass); Connection connRemote = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test",MainUser,MainPass); } catch(SQLException e){ } }// END OF WHILE
So, basically for each IP addresses (starting from aa.aa.a.aaa to gg.gg.g.ggg), I have a value 0 or 1 set in the IPStatus column in the database.
I want to basically establish connection to the above 7 databases only if the value of the IP Status is 1. So for example, if the value of IPStatus field is 1 for the IP addresses , namely, aa.aa.a.aaa,
bb.bb.b.bbb and dd.dd.d.ddd and for others it's zero, so I would like to establish connection to only first three. I am doing Something like the following inside the aforementioned try-block:
while(i<7){ try{ Connection[] MainConnection = new Connection[7]; Connection[] MainConnection = new Connection[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"; Connection connRemote = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test",MainUser,MainPass); String maindbsql = "SELECT IPStatus FROM Maindb.TableIPStatus "; Statement stmt = connRemote.createStatement(); Resultset rs = stmt.executeQuery(maindbsql); while(rs.next()){ int ipStatus = rs.getInt("IPStatus"); System.out.println("The value of ipStatus is:"+ipStatus); if(ipStatus == 1) { MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[i] + ":3306/test",RemoteUser,RemotePass); } }// END Of WHILE (rs.next()) }catch(SQLException e){ e.printStackTrace(); } i++; } END OF ORIGINAL WHILE LOOP while (i < 7)
But nothing happens after adding the additional code, not even the statement is getting printed `System.out.println("The value of ipStatus is:"+ipStatus);`
Please suggest where I am wrong. Thanks
--- Update ---
My schema is defined here: SQL Fiddle