0

I receive the error message (java.sql.SQLException) java.sql.SQLException: Before start of result set when I am trying to get a record from a MySQL database. This is the code I am using:

public TimelineJDBC(String name, String startTime){          
      final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
      final String DB_URL = "jdbc:mysql://localhost/TimeLineDB";
      final String USER = "root";
      final String PASS = "XXXX";
    try{
       Class.forName("com.mysql.jdbc.Driver");
       Connection con = DriverManager.getConnection(DB_URL, USER, PASS);

       String sql= "SELECT * FROM TIMELINE WHERE NAME= 'MyLife2' AND STARTTIME= '2008-02-01';";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(sql);

        this.timeline.setId(rs.getInt("ID"));
        this.timeline.setName(rs.getString("NAME"));
        this.timeline.setDescriprion(rs.getString("DESCRIPTION"));
        this.timeline.setStartTime(rs.getString("STARTTIME"));
        this.timeline.setEndTime(rs.getString("ENDTIME"));
        this.timeline.setPicture(rs.getString("PICTURE"));
        TimeUnit enumVal =  TimeUnit.valueOf(rs.getString("SCALE"));
        this.timeline.setScale(enumVal);

    }
    catch(SQLException ex){
        System.err.println("Error in retrieving a timeline record from database:"+ ex.getMessage());
    }
    catch (Exception e){
         System.err.println(e.getMessage()); 
    }  
}

Once trying to read the result set this.timeline.setId(rs.getInt("ID")); this SQLException is thrown: (java.sql.SQLException) java.sql.SQLException: Before start of result set

I am aware of the issue where you have to go to the next result set (rs.next();) but this doesn't seem to help, since the next result set is null. Here's a screenshot showing what is returned in the initial result set: https://i.sstatic.net/6NB6b.jpg

When trying to run the query manually on the same database, a correct ResultSet is formed, so it seems the problem doesn't lie with the database.

5
  • 5
    You have to call ResultSet.next() before you can access the data. ResultSet.next() does not return "the next result set". It returns a boolean indicating if there is another row available. It seems you are confusing this with Statement.getMoreResults() Commented May 22, 2014 at 13:02
  • I agree with what the above poster said. Just to add - if the result set comes empty then you may have another problem that. Do what they say first and report back. Commented May 22, 2014 at 13:13
  • What happens when you use A. Agarwal's solution? It looks correct. Commented May 22, 2014 at 13:17
  • By default resultset cursor will be pointing to before first row of the results. When you call ResultSet.next() only the internal resultSet cursor will be moved next row. and this call returns the whether next row is available or not. so please make sure that call the rs.next() Commented May 22, 2014 at 13:38
  • According to that comment on user3663519's answer, it doesn't seem to be about retrieving the result set anymore, but rather that the set is empty, even though that query, done manually, apparently returns a proper result. Commented May 22, 2014 at 17:55

2 Answers 2

4

You should use

while (rs.next()) { // for multiple rows
// get from rs here
}

or

if (rs.next()) { // for single row
// get from rs here
}
Sign up to request clarification or add additional context in comments.

Comments

1
while(rs.next()) {

    this.timeline.setId(rs.getInt("ID"));

    this.timeline.setName(rs.getString("NAME"));
    this.timeline.setDescriprion(rs.getString("DESCRIPTION"));
    this.timeline.setStartTime(rs.getString("STARTTIME"));
    this.timeline.setEndTime(rs.getString("ENDTIME"));
    this.timeline.setPicture(rs.getString("PICTURE"));

    TimeUnit enumVal =  TimeUnit.valueOf(rs.getString("SCALE"));

    this.timeline.setScale(enumVal);

}

if you kept this instead of that it will work

1 Comment

I used that loop, the control goes inside the loop but again after the line "this.timeline.setId(rs.getInt("ID"));" there will be an error which is : "e = (java.lang.NullPointerException) java.lang.NullPointerException"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.