Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Something wrong in my database SQL Logic(JDBC) , please correct me.

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Something wrong in my database SQL Logic(JDBC) , please correct me.

    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
    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".
    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
    Last edited by Jack_Tauson_Sr; March 27th, 2014 at 06:41 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Something wrong in my database SQL Logic(JDBC) , please correct me.

    catch(SQLException e){
     
     
    }

    Never ignore exceptions, which is what the above does. At the very least call printStackTrace() on the exception object so you can see the stack trace (with important information) on the command line. Post the stack trace here if it makes no sense.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Something wrong in my database SQL Logic(JDBC) , please correct me.

    Thanks for your answer, I have printStackTrace() in my real code (updated the same here as well) but still wondering about the logic that I am using there. There are no exceptions thrown by my code.
    Last edited by Jack_Tauson_Sr; March 27th, 2014 at 06:45 PM.

Similar Threads

  1. Replies: 2
    Last Post: March 13th, 2014, 02:24 PM
  2. sql lite database
    By simbarashe in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 19th, 2013, 07:03 AM
  3. Inserting into sql express using jdbc
    By tendaimare in forum JDBC & Databases
    Replies: 1
    Last Post: April 27th, 2012, 11:31 AM
  4. Replies: 1
    Last Post: June 14th, 2011, 11:08 AM
  5. sql/jdbc parser
    By anand.stk in forum Member Introductions
    Replies: 1
    Last Post: January 27th, 2011, 09:43 AM