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: Problem with accessing result set with postgresql

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    Madurai
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with accessing result set with postgresql

    Hi friends,

    I have successfully installed the JDBC driver for PostgreSQL DB. I have checked by connecting woth DB. It is running successfully. Now I have two tables namely TABLE1 and TABLE2. TABLE1 has columns as IP and count. TABLE2 has IP only as column. Now i want to insert each IP values from TABLE1 into TABLE2 with its maximum count value since TABLE1 will have same IP with different count values.

    I have written the code for that.
     
    Class.forName("org.postgresql.Driver"); //load the driver
    db = DriverManager.getConnection("jdbc:postgresql:"+database,username,password);
    sql=db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
    ResultSet ip_list = sql.executeQuery("select ip,count from TABLE1 where count in(select max(count) from TABLE1 group by src_ip);");
     
        while(ip_list.next())
        {
    	src_ip=ip_list.getString("src_ip");
    	count=ip_list.getInt("count");
     
    	sqlText = "insert into TABLE2 values('" + src_ip + "'," + hop_count + ")";
    	//System.out.println(sqlText);
    	sql.executeUpdate(sqlText);
    	//System.out.println(src_ip+"  "+hop_count);
    	}


    It gives the following errror "Exception in thread "main" org.postgresql.util.PSQLException: This ResultSet is closed"
    The result set ip_list has three rows, but only one row is inserted in TABLE2 and then the above error has been thrown. When i comment the sql.executeUpdate() statement and uncomment the printing statement all the 3 row values get printed.

    Where is the problem? What mistake i have made? Please clarify this.

    With regards,
    Raja Pandi
    Last edited by vrp; December 21st, 2010 at 02:08 AM.


  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: Problem with accessing result set with postgresql

    A close read of the API for Statement reveals the following line "All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists." So the execute in the loop should be performed with a different Statement than the one it loops over

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Location
    Madurai
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with accessing result set with postgresql

    Ya, I understood and solved my problem using two separate 'statements'. Thanks for help brother. :-)

Similar Threads

  1. [NEED HELP] to clear result on my tic-tac-toe game
    By bontet in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2010, 03:50 PM
  2. result set array button
    By dread_arisawa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2010, 10:05 AM
  3. [URGENT] - Problem accessing web sites with Java!
    By jguilhermemv in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 04:43 PM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  5. Problem of getting result than SQL to JTable
    By MS_Dark in forum Exceptions
    Replies: 1
    Last Post: March 10th, 2009, 06:26 AM

Tags for this Thread