Take the 2-minute tour ×

I get an error stating that I got an exception before start of a result set. I'm trying to get a value (score from the MySQL database) and add one to the Java rank based on the player score. This is to create a scoreboard.

So if the player's score is lower than the current score, it gets posted with rank 1. If it's higher, the program checks the score against the next entry in the MySQL database. I haven't yet implemented a feature to change all the current entries rank's to increment by 1.

Bottom Line: I'm creating a scoreboard using MySQL and Java. The Java program creates a score entry based on input, and then sends it off to the MySQL database.

      System.out.println("Your score is: "+score*2+"  (A lower score is better.)");
      try {
   // create a java mysql database connection
   String myDriver = "com.mysql.jdbc.Driver";
   String myUrl = "jdbc:mysql://";
   String dbName = "apesbridge2013";
   String tbName = period + "period";
   Connection conn = DriverManager.getConnection(myUrl, "user", CENSORED);
   next = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
   ResultSet resultSet = next.executeQuery("SELECT * FROM " + tbName);
   int cscore = resultSet.getInt("score");
   for(int sscore = score; sscore > cscore;){
       cscore = resultSet.getInt("score");

   stmt = conn.createStatement();
   stmt.executeUpdate("insert into " + dbName + "." + tbName + " " + "values(" + rank + ", '" + name + "', " + score + ")");
 catch (Exception e)
   System.err.println("Got an exception! ");

share|improve this question
What is that exception, can you provide that to us? –  demongolem Jan 18 '13 at 20:00
and also, you can get the rank by counting the number of records that have lower score .. it would be much more effective then comparing in java against all the records in the database –  Majid L Jan 18 '13 at 20:03
There's a substantial answer to this old question from @spencer7593 but you've neither replied nor upvoted. Please consider replying in some form when you receive help, if only to encourage the sort of helpful people we need on here! –  halfer Mar 27 at 17:50

3 Answers 3

Put resultSet.next(); right below your executeQuery line.

share|improve this answer

As stated by @hd1, you need to call ResultSet.next() after the call to executeQuery:

while (resultSet.next()) {

Also, better to use PreparedStatement instead of java.sql.Statement and use parameter placeholders to protect against SQL Injection attacks:

share|improve this answer

There's a problem in your for loop; the exit condition should be when there are no more rows to fetch. Your query doesn't guarantee that the exit condition will ever be met, and you may attempt to fetch past the end of the resultset. (And even when your for loop does happen to be entered, and when if the for loop does happen to be exited, the rank value derived by that loop is non-deterministic, it's dependent on the order that rows are returned by the database.

I also don't see any call to resultSet.close() or next.close().

There's so many problems here, it's hard to know where to begin.

But firstly, it would be much more efficient to have the database return the rank to you, with a query:

"SELECT COUNT(1) AS rank FROM " + tbName + " WHERE score < " + score 

rather than pulling back all the rows back, and comparing each score. That's just painful, and a whole lot of code that is just noise. That would allow you to focus on the code that DOES need to be there.

Once you get that working, you need to ensure that your statement is not vulnerable to SQL injection, and prepared statements with bind variables is really the way to go there.

And you really do need to ensure that calls are made to the close() methods on the resultset, prepared statements, and the connection. We typically want these in a finally block. Either use nested try/catch blocks, where the variables are immediately initialized, like this:

try {
    Connection conn = DriverManager.getConnection(...

    try {
        stmt = conn.CreateStatement();

        String query = "SELECT COUNT(1) AS `rank` FROM " + tbName + " WHERE `score` < " + score ;

        try {
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
               rank = rs.getInt("rank");
        } finally {
            if (rs!=null) { rs.close() };
    } finally {
        if (stmt!=null) { stmt.close() };
} finally {
  if (conn!=null) { conn.close() };

Or one big try/catch block can also be workable:

} finally {
   if (resultSet!=null) { resultSet.close() };
   if (next!=null) { next.close() };

The point is, the close methods really do need to be called.

share|improve this answer

Your Answer


