このコードを実行するたびに、このエラーは次のようになります。. .!
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)
以下は、私が実行してこのエラーをキャッチした main のコードです。. .
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);
con (DBConnection クラスのインスタンスです)。. . readDatabase メソッドは DBConnection クラス内にあります。
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;
}
}