Question

Whenever i run this code this error follows like . . .!

Exception in thread "main" java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5650)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at com.mysql.jdbc.ResultSetImpl.getDoubleInternal(ResultSetImpl.java:2463)
at com.mysql.jdbc.ResultSetImpl.getDouble(ResultSetImpl.java:2424)
at com.mysql.jdbc.ResultSetImpl.getDouble(ResultSetImpl.java:2442)
at Library.main(Library.java:157)

The following is the code from main that i run and caught this error. . .

    System.out.println("Registration number of member returning book: ");
        String number = input.nextLine();

        System.out.println("Book shelf number");
        double bookNumber = input.nextDouble();

        double shelfNo;
        String borrower;
        long timeBookTaken;
        long timeToReturnBook;

        if(borrow.hasBeenBorrowed(bookNumber))
        {
            String sql = "SELECT * FROM borrowed WHERE shelfNumber = '"+ bookNumber +"'";
            ResultSet rs = con.readDatabase(sql,statement);



                //Statement statmnt = con.getStatement();
                shelfNo = rs.getDouble("shelfNumber");
                borrower = rs.getString("borrowerRegNum");
                timeBookTaken = rs.getLong("timetaken");
                timeToReturnBook = rs.getLong("time_to_return");

                System.out.println(shelfNo+"\t"+borrower+"\t"+timeBookTaken+"\t"+timeToReturnBook);

The con(is an instance of DBConnection class). . . readDatabase method is inside DBConnection class.

DBConnection class
===================


     package database;

    import java.sql.*;

    public class DBConnection
    {

        private static Connection connection;
        private static Statement statement;
        private static ResultSet resultset;

private static String mysqlURL = "jdbc:mysql://localhost/library";
private static String mysqlUsername = "root";
private static String mysqlPassword = "";

public DBConnection()
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        connection
                 =   DriverManager.getConnection(mysqlURL,mysqlUsername,mysqlPassword);
        statement = connection.createStatement();

    }
    catch(Exception exception)
    {
        System.out.println("Error: "+ exception.getMessage());
    }

}

public Connection getConnection()
{
    return connection;
}


public Statement getStatement()
{
    return statement;
}

public ResultSet readDatabase(String sql,Statement statement) throws SQLException
{
    resultset = statement.executeQuery(sql);

    return resultset;
}

}

Was it helpful?

Solution

You haven't called next() on the result set yet. Not only does that tell you if there's another row in the result set, it advances the result set cursor to that row.

Enclose your "get" calls to the result set in

while (rs.next()) {

    // get calls here

}

so you process all rows, advancing to each row each loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top