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

    Default restricting my loop to specific IP addresses

    Please refer to following code:

    Java 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";
      
                                Statement stmt = connRemote.createStatement();  
      
                                Resultset rs = stmt.executeQuery(maindbsql);  
                                                         
                                while(rs.next()){     
      
                                        String ipAddress = rs.getString("IP_vch");                     
                                  
                                        System.out.println("The value of ipAddress is:"+ipAddress);  
      
                                         
                                            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)
    The output of the following statement :
    Java Code:
    System.out.println("The value of ipAddress is:"+ipAddress);
    is: bb.bb.b.bbb and cc.cc.c.ccc.
    Therefore I wish to consider only these two IP addresses for opening connection on the following line:
    Java Code:
    MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" 
          + RemoteIPAddress[i] 
          + ":3306/test",RemoteUser,RemotePass);
    But since I have already defined, RemoteIPAddress array from 0 - 6 at the start, the connection is getting opened for all the IP address. Please advise how can I restrict myself to some specific IP addresses as discussed above.

    For reference, here is my database schema:

    SQL Fiddle

    Thanks
    Last edited by Tolls; 03-28-2014 at 10:52 AM. Reason: Tidying up post

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: restricting my loop to specific IP addresses

    If all you want to do is make an array of connections to whatever ip addresses are returned from the database then there's no point your code holding an array of possible ip addresses. You are, after all, getting them from the database.
    Also your MainConnection[] probably ought to be a List<MainConnection> as you don't actually know how many connections you need.

    Then simply add connections to that array, one for each ip address found in the db.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: restricting my loop to specific IP addresses

    Quote Originally Posted by Tolls View Post
    Then simply add connections to that array, one for each ip address found in the db.
    (array = list)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: restricting my loop to specific IP addresses

    Whoops.
    Thanks for clarifying.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Restricting size to a jtextbox
    By wired-in=p in forum AWT / Swing
    Replies: 2
    Last Post: 03-06-2012, 05:16 AM
  2. Arrays memory addresses
    By Dayanand in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 10:17 AM
  3. sdk development: restricting access from outside
    By madhusudhan y in forum Advanced Java
    Replies: 1
    Last Post: 03-01-2011, 05:20 PM
  4. Replies: 2
    Last Post: 11-08-2008, 11:40 AM

Posting Permissions

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