Results 1 to 8 of 8
Thread: need help with ResultSets !!!
- 01-20-2011, 10:54 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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 !!!!!Last edited by ady_bavarezu89; 01-20-2011 at 10:56 PM.
- 01-21-2011, 07:51 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 01-21-2011, 11:07 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 01-21-2011, 11:16 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What error (the complete stacktrace), and post that code.
- 01-21-2011, 11:37 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
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)
- 01-21-2011, 11:46 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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).
- 01-21-2011, 11:50 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 01-21-2011, 12:15 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
JDBC problem with multiple resultsets on mysql database: getUpdateCount() always zero
By plica10 in forum JDBCReplies: 3Last Post: 02-02-2010, 01:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks