Home arrow static arrow Java Programming [Archive] - ResultSet methods dont work...?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - ResultSet methods dont work...?
This topic has 60 replies on 5 pages.    1 | 2 | 3 | 4 | 5 | Next »

Posts:343
Registered: 7/13/04
ResultSet methods dont work...?  
Aug 2, 2004 9:38 AM



 
I'm trying to get the primary key for the the row that I insert into the database. every time i use any of the ResultSet methods the program goes all haywire goes into some kind of infinant looop (at least thats what it seemd like when i debugged it line-by-line) here's my code: (by the way i'm using MySQL)

                sql = new Access("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","nthompson","123");------------------------------------------------------------------------------------------------------------------------------  public static ResultSet ins(String sql) throws Exception {  Statement stmt = null;  int rows = 0;  if (dbConn == null)    throw new Exception("No database connection");  try {     stmt = dbConn.createStatement();    rows = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);     key = stmt.getGeneratedKeys();   } catch (Exception error) {     System.out.println(error);   }  stmt.close();   return key;}--------------------------------------------------------------------------------------------------------------------------------------                               rs = sql.ins("INSERT INTO FrameData (" +                                                "FrameNumber, MessageId, PlatformId, TaskId, ParentId, TimeOfDay" +                                                        ") VALUES (" +                                                        frame + "," +                                                        mid + "," +                                                        pid + "," +                                                        tid + "," +                                                        paid + "," +                                                        day + ")");//                                    rs.getInt("id");                    //      THIS IS THE ROW THAT SCREWS UP//                                    sql.gStmt.close();                                  System.out.println("key " + rs);

the rs.getInt("id") row screws up, but if i replace that with ANY method from the ResultSet class it screws up. there is no exception or anything like that, it just goes in some kind of infinant loop and it doesn't execute anything else in the program. when i comment the getInt method, the rest of the program works, but i need the ResultSet rs value to be an int value, because as it is it gives me the value: com.mysql.jdbc.ResultSet@1fd6bea and that is useless to me
 

Posts:6,487
Registered: 5/5/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:41 AM (reply 1 of 60)



 
When you close a statement you close the resultset.
What I do is have a method which returns a Statement which I call exec on and close. The close should be in a finally block. Note Errors are not Exceptions so throwing an error would not result in stmt.close() being called.
 

Posts:37,103
Registered: 3/30/99
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:41 AM (reply 2 of 60)



 
I think the problem is because you're closing the statement and then trying to use its ResultSet. Don't close the statement until you're done with the ResultSet.
 

Posts:37,103
Registered: 3/30/99
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:42 AM (reply 3 of 60)



 
Also, close() for statement, ResultSet, any sockets you might open, etc., should almost always be in a finally block, so that it gets called regardless of how the try block completes.
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:45 AM (reply 4 of 60)



 
if you've noticed i have commented gstmt, i've tried closing it at the end like this:

                               rs = sql.ins("INSERT INTO FrameData (" +                                                "FrameNumber, MessageId, PlatformId, TaskId, ParentId, TimeOfDay" +                                                        ") VALUES (" +                                                        frame + "," +                                                        mid + "," +                                                        pid + "," +                                                        tid + "," +                                                        paid + "," +                                                        day + ")");                                    rs.getInt("id");                                    sql.gStmt.close();          // it still gives me problems when i change to to this                                  System.out.println("key " + rs);
 

Posts:37,103
Registered: 3/30/99
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:51 AM (reply 5 of 60)



 
Did you take out the other call to close? What are you doing if there's an exception?

Please post the current code and the current error. Don't make us piece it together incrementally.
 

Posts:6,487
Registered: 5/5/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:54 AM (reply 6 of 60)



 
sql.gStmt.close(); // it still gives me
problems when i change to to this
System.out.println("key " + rs);
You are still trying to use the result set after it has been closed .
I repeat, closing the statement closes the resultset..
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:57 AM (reply 7 of 60)



 
[code]
sql = new Access("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","nthompson","123");


public static ResultSet ins(String sql) throws Exception {
int rows = 0;
if (dbConn == null)
throw new Exception("No database connection");
try {
gStmt = dbConn.createStatement();
rows = gStmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
key = gStmt.getGeneratedKeys();
} catch (Exception error) {
System.out.println(error);
}
// stmt.close();
return key;
}

rs = sql.ins("INSERT INTO FrameData (" +
"FrameNumber, MessageId, PlatformId, TaskId, ParentId, TimeOfDay" +
") VALUES (" +
frame + "," +
mid + "," +
pid + "," +
tid + "," +
paid + "," +
day + ")");
rs.getInt("ID"); // this part is the part that goes crazy (stops the program)
sql.gStmt.close();
System.out.println("key " + rs);

there, thats the code, (i've labled the part that stops the program)
 

Posts:37,103
Registered: 3/30/99
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:59 AM (reply 8 of 60)



 
> rs.getInt("id");<div class="jive-quote">sql.gStmt.close();          // it still gives meproblems when i change to to thisSystem.out.println("key " + rs);

You're printing out rs. Calling rs.getInt doesn't cause rs's String value to be the int you just got. You need to do
int id = rs.getInt("id");System.out.prinln(id); 
You can close the ResultSet immediately after the getInt (if you're not going to get anything else from it).
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 9:59 AM (reply 9 of 60)



 
oops, switch these last two lines, the problem is that the program doesn't even get this far though, it just stops working at the part where i call the method : rs.getInt("ID");

sql.gStmt.close();
System.out.println("key " + rs);
 

Posts:37,103
Registered: 3/30/99
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:01 AM (reply 10 of 60)



 
So nothing executs after calling getInt? Are you sure? How can you tell? How are you handling exceptions that might occur there? Make sure you're printing the stack trace, not smothering them.
 

Posts:6,750
Registered: 1/25/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:02 AM (reply 11 of 60)



 
It doesn't just "stop working." If jverd's suggestion doesn't clear anything up (and even if it doesn't you still need to do that) then chances are you're swallowing an execption. If you have something that looks like this
catch(Exception e) {}
Replace it with this
catch(SQLException e){  e.printStackTrace();}
 

Posts:6,750
Registered: 1/25/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:02 AM (reply 12 of 60)



 
When I said "jverd's suggestion" I meant reply 8...
 

Posts:343
Registered: 7/13/04
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:03 AM (reply 13 of 60)



 
yea this whole thing is surrounded by a:

[code]
try {

}catch (EOFException error) {
error.printStackTrace();
}
[code]

the only reason i have any idea what it's doing is because i debug it line by line, i hold down the "go to next line F(key), and it goes int like 30 diff. classes and get stuck on this one loop
 

Posts:13,769
Registered: 00-11-29
Re: ResultSet methods dont work...?  
Aug 2, 2004 10:09 AM (reply 14 of 60)



 
What is the relationship between these two snippets of code?
 
This topic has 60 replies on 5 pages.    1 | 2 | 3 | 4 | 5 | Next »