我有这个代码可以从我的数据库中的帐户表中获取金额 但我收到此错误:耗尽结果集
public int accountAmount(int custID){
int amount=0;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl" , "MYATM","myjava123");
PreparedStatement statment = connection.prepareStatement("select amount from Account where cust_id=?");
statment.setInt(1,custID);
ResultSet resultSet=statment.executeQuery();
resultSet.next();
amount= resultSet.getInt("amount");
statment.close();
connection.close();
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
return amount;
}
最佳答案
检查 ResultSet#next() 的返回类型;剧透 - 这是一个 boolean 值(如果为 false)表示您已经过了最后一行。所以,而不是这个
resultSet.next();
amount= resultSet.getInt("amount");
你需要这个!
if (resultSet.next()) {
amount= resultSet.getInt("amount");
}
关于java - 耗尽结果集错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294738/