• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Exception when try to use resultset

 
Mike Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Harris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I tried that and now I get the following error:

java.sql.SQLException: After end of result set
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34870
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
That shouldn't be happening. Is it possible rs.next() is in the code twice?

Can you post the revised code snippet?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic