| Author |
SQL Exception when try to use resultset
|
Mike Harris
Greenhorn
Joined: Oct 13, 2005
Posts: 5
|
|
I am getting the following error: java.sql.SQLException: Before start of result set. The problem is occurring at: c = rs.getInt("current_connections"); Below is my code: import java.sql.* import java.io.*;//delete import java.util.*;//delete //This class reads Master.txt file, so contents may be modified by writer class Reader1 { public Reader1(String command){ System.out.println("reader 1 has been called"); try { int c; int m; Statement stmt; System.out.println("reader 1 before sql"); Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/jdbase?user=xxx&password=xxx"); String query = "SELECT current_connections, max_connections FROM master WHERE compid ='xxx'"; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); System.out.println("reader 1 after sql"); c = rs.getInt("current_connections"); m = rs.getInt("max_connections"); stmt.close(); conn.close(); System.out.println("got sql values"); //add c and m to an array called mast_arr Chat6.mast_array[0]=c; Chat6.mast_array[1]=m; }//end try catch (Exception ex) { System.out.println(ex); } } }
|
 |
Tom Blough
Ranch Hand
Joined: Jul 31, 2003
Posts: 263
|
|
In a new result set, the cursor is positioned before the first record. To access the returned records you should do something like this: rs.next() will return true if the cursor was sucessfully positioned on a record. You can then use the getxxx methods to retrieve the field data. If you are expecting just one row to be returned by the query you can leave out the while, but you still need to call next() and check the return code. Cheers, [ October 13, 2005: Message edited by: Tom Blough ]
|
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
|
 |
Mike Harris
Greenhorn
Joined: Oct 13, 2005
Posts: 5
|
|
Ok, I tried that and now I get the following error: java.sql.SQLException: After end of result set
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26144
|
|
Mike, That shouldn't be happening. Is it possible rs.next() is in the code twice? Can you post the revised code snippet?
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: SQL Exception when try to use resultset
|
|
|