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

    Default making my resultset Updatable and inserting record in table using hashmap

    Please consider the 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";    
    							
    							Map<String, Connection> connections = new HashMap<>();
        
                                Statement stmt = connRemote.createStatement();    
        
                                Resultset rs = stmt.executeQuery(maindbsql);    
                                                           
                                while(rs.next()){       
        
                                        final String ipAddress = rs.getString("IP_vch");                       
                                        
                                        System.out.println("The value of ipAddress is:"+ipAddress);    
                                        connection.put(ipAddress,DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass));
                                           
                                             
                                            
                                        
                                    }// END Of WHILE (rs.next()) 
    								
    								//MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + RemoteIPAddress[i] + ":3306/test",RemoteUser,RemotePass);   
    
                                String QueryString = "SELECT  Query goes here";
    							
    							SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    
                                SelectResultsRS = SelectResultsStmt.executeQuery(QueryString);
    							
    							    while(SelectResultRS.next())
    								{
    								    String QueryStringInsertFromRemote = "INSERT INTO MY TABLE"
                        									                 "("
    																		  +"IP_vch" 
    																		  +" ) "
    																		  
    																		+"VALUES" 
    																		 +"("
    																		
    																		   + " '" +  RemoteIPAddress[i] + "' "
    								                                         +")";
    								
    								
    								
    								}
    
                                							
                                
                                
                            }catch(SQLException e){    
                                
                            e.printStackTrace();    
                                
                            }    
        
        i++;    
        } END OF ORIGINAL WHILE LOOP while (i < 7)

    As seen in some part of my commented code, I was using MainConnection and harcoding the IP addresses before. But since, my query is getting all the eligible(IPStatus = 1) IP addresses,I don't need to hardcode them anymore
    and hence I am using Map interface to store the eligible IP addresses as shown above.

    Problem I am encountering are as follows:

    1) When I was using MainConnection and hardcoded IP addresses, I was using something like the following to make my resultset updatable:

    Java Code:
    SelectResultsStmt = MainConnection[i].createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    But, considering the fact that I am no longer using the Mainconnection thing, how should I do the same thing with the Resultset now using the HashMap?


    2) Similarly, if we go further down the code, in the QueryStringInsertFromRemote , I am inserting the value of the IP address for which the connection has been established at that very moment. Please let me know how can I
    overcome with these problem as I am using HashMap here?

    3) 1) Considering the fact that I am not hardcoding IP addresses, do I need the while loop now?

    Thanks

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

    Default Re: making my resultset Updatable and inserting record in table using hashmap

    Java Code:
    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";    
    Map<String, Connection> connections = new HashMap<>();
    Statement stmt = connRemote.createStatement();    
    Resultset rs = stmt.executeQuery(maindbsql);
    while(rs.next()){       
        final String ipAddress = rs.getString("IP_vch");
        System.out.println("The value of ipAddress is:"+ipAddress);    
        connection.put(ipAddress,DriverManager.getConnection("jdbc:mysql://" 
                + ipAddress 
                + ":3306/test",RemoteUser,RemotePass));
    }// END Of WHILE (rs.next()) 
    
    String QueryString = "SELECT  Query goes here";
    SelectResultsStmt = MainConnection[i].
            createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
    
    SelectResultsRS = SelectResultsStmt.executeQuery(QueryString);
    while(SelectResultRS.next()) {
        String QueryStringInsertFromRemote = "INSERT INTO MY TABLE"
                        					+"IP_vch" 
    							+" ) "
    							+"VALUES" 
    							+"("
    							+ " '" +  RemoteIPAddress[i] + "' "
    							+")";
    }
    Right, just done this so the code is actually viewable on the page.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: making my resultset Updatable and inserting record in table using hashmap

    1. Why do you need to make the ResultSets updateable? That's often a flag (for me) that you really want to be doing a simple UPDATE. But if you really need to, then you have your Connection objects in the Map.

    2. Is your problem that you don't know how to iterate over the values in a Map? In fact, do you actually need a Map, or could you simply go for a List?

    3. Which while loop? You have two.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 04-03-2013, 04:33 PM
  2. calculate making problem while using while() loop in ResultSet
    By raj.mscking@gmail.com in forum New To Java
    Replies: 6
    Last Post: 06-18-2012, 08:30 AM
  3. Not getting 1st record from table.
    By abhi7080 in forum New To Java
    Replies: 5
    Last Post: 01-11-2012, 11:14 AM
  4. Updating a JTable after Inserting Record
    By muhindi in forum Advanced Java
    Replies: 4
    Last Post: 01-24-2011, 12:48 PM
  5. problem with inserting new record
    By gerard kowara in forum JDBC
    Replies: 1
    Last Post: 10-26-2010, 11:33 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
  •