Results 1 to 15 of 15
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default What's wrong with the following conditional check check?

    Java Code:
    Connection[] MainConnection = new Connection[7];
    String[] RemoteIPAddress = new String[7];
    
    while (i < 7){
    	
    		try{           
    		               
    						
    						
    	
    						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:


    Java Code:
                         Connection[] MainConnection = new Connection[7];
    						
    		     String[] RemoteIPAddress = new String[7];
    			while(i<7){
                      try{			
    						
    							
    							
    							
    							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){
    						
    						
    						}
    					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

    My database schema can be seen here:

    http://sqlfiddle.com/#!2/8cce4
    Last edited by Jack_Tauson_Sr; 03-28-2014 at 04:58 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: What's wrong with the following conditional check check?

    Is your that catch block really empty in your real code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: What's wrong with the following conditional check check?

    Hi,

    you should move stuff that should only be executed once outside the while loop.

    - MainConnection is initialized inside while so it is not available outside the loop and a connection is gone on the next walk through the loop.
    - your query on connRemote might only be required once. Then you should do it outside the loop, too.
    - on a resultset you normaly go to the first entry with a next call. If you are just interested in the first row, then a if (rs.next()) { ... } is recommend.

    With kind regards,

    Konrad

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    I would rather see an answer to Toll's question first, Konrad. The answer might give a reason why the OP can't figure this out himself, which would be a far more valuable problem to solve.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: What's wrong with the following conditional check check?

    Yeah, the point of Toll must be handled first because that will show the error that occurred, Maybe I should have mentioned that my points are meant just as an addition.
    (If the connect was successful the missing rs.next() might be the problem. But that is just guessing around which could be changed with a simply System.out.println call inside that catch that shows the exception.)

    With kind regards,

    Konrad

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: What's wrong with the following conditional check check?

    I just get twitchy when I see blocks like that. Sometimes it's just the poster reducing the amount of code posted on the forum, but mostly it really is empty catch blocks.
    printStackTrace() is your friend.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    Quote Originally Posted by Tolls View Post
    I just get twitchy when I see blocks like that. Sometimes it's just the poster reducing the amount of code posted on the forum, but mostly it really is empty catch blocks.
    printStackTrace() is your friend.
    :)
    No SLF4J is ;)

    And btw: I think you just have a vitamin D deficiency.
    Last edited by gimbal2; 03-27-2014 at 01:17 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    Well, I have noticed something that no one else has mentioned in his code and it has nothing to do directly with SQLExceptions. But I will follow your lead and not mention it. And since this is just a piece of his code, I may be wrong anyway.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    Just say it dude, you're obviously almost bursting. Something about wanting to create X different connections, but the code only ever connects to one single address in its current state perhaps?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    Well, I don't know. Mine was more simple. I do not know the value of "i" so I don't even know if it entered the loop. And it certainly won't exit because the variable is never incremented (at least I couldn't find it). The SQL stuff is beyond my current knowledge base. Although his print statement probably isn't being printed probably because an exception is being thrown and bypassing the print statement (hence, don't ignore catch blocks).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: What's wrong with the following conditional check check?

    We're on the same page ;) Yeah indeed, missing code is missing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: What's wrong with the following conditional check check?

    Thanks all for your answers, I added another catch block

    catch(SQLException e){
    e.printStackTrace();

    }
    And found out that I am getting "java.lang.NullPointerException" at the line 27 in the aforementioned code. That means it's not able to get the value of IPStatus and I am trying to use that on line 28. How should I correct this problem?

  13. #13
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: What's wrong with the following conditional check check?

    Okay, so my code is running now. I have updated my question with the running code. The poblem I notices is now with my SQL Query which doesn't makes sense with the following comparision I am doing in my code. Because when I checked the Query
    SELECT IPStatus FROM Maindb.TableIPStatus , I just gor 7 rows and one columns with values 0,0,0,0,0,1,1. And when I am comparing it with 1 in my code, it doesn't make any sense. Do I need to follow some other approach?

  14. #14
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: What's wrong with the following conditional check check?

    Hi,

    that Problem is now SQL related and not Java related.

    You just made a query
    SELECT IPStatus FROM Maindb.TableIPStatus
    So you simply get the IPStatus from that table.

    If you need more columns, then you have to mention them inside the SELECT statement e.g.
    SELECT IPStatus, IPAddressColumnName FROM Maindb.TableIPStatus

    If you only want a specific row, then you can add a WHERE clause:
    SELECT IPStatus FROM Maindb.TableIPStatus WHERE IPAddressColumnName = 'aa.bb.cc.dd'

    If you have a database with other databases where you want to do something, then you might want to drop the hardcoded IP-Adresse and simply do something like
    - Query SELECT IPAddressColumnName FROM Maindb.TableIPStatus WHERE IPStatus=1
    - while (rs.next())
    --> Read the IP Address and use it to connect to the database and so on.

    But all this depends on your database. If the IPStatus does not have the IPAddress, then you might need to join in other tables and so on.

    With kind regards,

    Konrad

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: What's wrong with the following conditional check check?

    I'm not sure I understand.
    What are you seeing output from the code? (Copy and paste the output from the println)
    What are you expecting to see? (This might involve posting the TableIPStatus IPStatus values from the db)

    And you still seem to have an empty catch block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. help me check any wrong!
    By jimmy91 in forum New To Java
    Replies: 7
    Last Post: 11-16-2010, 12:54 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •