• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL Exception when try to use resultset

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I tried that and now I get the following error:

java.sql.SQLException: After end of result set
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic