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.
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 withStatement.getMoreResults()