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.


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

Results 1 to 2 of 2

Thread: restricting my loop to specific IP addresses

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

    Default restricting my loop to specific IP addresses

    Please refer to following 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);  
                                        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){  
        } END OF ORIGINAL WHILE LOOP while (i < 7)

    The output of the following statement :
    [b] System.out.println("The value of ipAddress is:"+ipAddress);  [/b] 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:
    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 688 Times in 675 Posts

    Default Re: restricting my loop to specific IP addresses

    But since I have already defined, RemoteIPAddress array from 0 - 6 at the start . . .
    Wait to define the RemoteIPAddress array until you know which addresses it should contain, OR don't populate the array with ALL possible addresses, only those that are valid. If you don't know in advance how many of the possible addresses will be valid, use a variable length data structure, perhaps an ArrayList.

Similar Threads

  1. Replies: 1
    Last Post: November 17th, 2013, 08:38 PM
  2. Replies: 1
    Last Post: November 16th, 2013, 12:35 PM
  3. Getting Distance between two addresses
    By Shockwave786 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 23rd, 2012, 10:19 AM
  4. [SOLVED] restricting a particular key stroke or key code
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 22nd, 2011, 11:19 AM
  5. Compress Ipv6 Addresses
    By abhay8nitt in forum Java Networking
    Replies: 5
    Last Post: May 20th, 2010, 06:58 AM