Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default need help with ResultSets !!!

    Hey everyone...I have a little problem that I just can't solve alone :(

    After struggling a lot with jdbc drivers and stuff....I finally managed to connect eclipse to postgresql...but another problem occured...I attached 2 pictures..one with my table in pgadmin and one with the code in eclipse....so what I want to do is that :
    I need the first row (12, sda ,12) to do something with it...and the second row(23, a ,21) to do something else with it..like to separate them...I tried different things like making 2 ResultSet variables and 2 different while loops:

    res = sqlCommand.executeQuery("SELECT * FROM adi WHERE num = 1;");
    res2 = sqlCommand.executeQuery("SELECT * FROM adi WHERE num = 2;");

    while(res.next()){...}
    while(res2.next()){...}

    ...but that didn't worked..so has anyone any idea how I could access the 2 rows separately???...I'm working on a GUI for a bigger project and I need separate rows to insert the data from each row to a different player(it's a football database) in a different window(JFrame)

    Thanks very much in advance !!!!!
    Attached Thumbnails Attached Thumbnails need help with ResultSets !!!-print1.jpg   need help with ResultSets !!!-print2.jpg  
    Last edited by ady_bavarezu89; 01-20-2011 at 10:56 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As the API docs clearly state when you execute a query with a statement all currently open resultsets already associated with that statement will be automatically closed. Use two statement objects.

  3. #3
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    As the API docs clearly state when you execute a query with a statement all currently open resultsets already associated with that statement will be automatically closed. Use two statement objects.
    when I try to use 2 statement objects it gives me an error :(...could you explain it to me a bit more specific please ??

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What error (the complete stacktrace), and post that code.

  5. #5
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class b {

    public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{

    Class.forName("org.postgresql.Driver").newInstance ();
    String URL ="jdbc:postgresql://localhost:5432/bab";
    String userName = "postgres";
    String password = "adi";
    Connection conn = DriverManager.getConnection(URL,userName,password) ;
    Statement sqlCommand = conn.createStatement();

    ResultSet res; // collection for sql results
    ResultSet res2;
    Integer num; // variable for database values
    String title; // variable for database values
    Integer responsible;

    res = sqlCommand.executeQuery("SELECT * FROM adi WHERE num = 1;");
    res2 = sqlCommand.executeQuery("SELECT * FROM adi WHERE num = 2;");

    while (res.next()) { // access attribute values of current tuple
    num = res.getInt("num");
    title = res.getString("title");
    responsible = res.getInt("responsible");

    System.out.println("1\t" + num + "\t" + title + "\t" + responsible);
    }
    }
    }

    Even if I try this ....I created a new resultSet object to store the values of the table but didn't used them yet...and when I compile I get this :

    Exception in thread "main" org.postgresql.util.PSQLException: This ResultSet is closed.
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkC losed(AbstractJdbc2ResultSet.java:2674)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.next(A bstractJdbc2ResultSet.java:1806)
    at b.main(b.java:26)

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I said to use two statements but you are still using "sqlCommand" for both of the queries, and that exception is exactly what occurs from doing that (which I had already explained).

  7. #7
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    I said to use two statements but you are still using "sqlCommand" for both of the queries, and that exception is exactly what occurs from doing that (which I had already explained).
    oooh ..I'm sorry...I didn't quite got you the first time...but now I'll try ..I think I understood what you meant

  8. #8
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Hey ...it worked !!!!!! thanks soooo much..I really appreciate it...I know I'm a noob with this but I kinda taught myself so that's why I'm not really experienced :o....but thanks a lot again !!

Similar Threads

  1. Replies: 3
    Last Post: 02-02-2010, 01:33 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
  •