File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes SQL Exception when try to use resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "SQL Exception when try to use resultset" Watch "SQL Exception when try to use resultset" New topic

SQL Exception when try to use resultset

Mike Harris

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.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");

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");


System.out.println("got sql values");

//add c and m to an array called mast_arr
}//end try
catch (Exception 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: 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.

[ 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

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
author & internet detective

Joined: May 26, 2003
Posts: 33102

That shouldn't be happening. Is it possible is in the code twice?

Can you post the revised code snippet?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: SQL Exception when try to use resultset
It's not a secret anymore!