gpt4 book ai didi

java - 无法从 sqlite 表获取记录,出现 ResultSet Closed 错误

转载 作者:行者123 更新时间:2023-12-01 09:10:17 25 4
gpt4 key购买 nike

public void actionPerformed(ActionEvent e) {
try{
String searchQuery = "select BookTitle,ISBN,Price,IsAvailable from BookDetails where BookTitle =?";
PreparedStatement serPST = connector.prepareStatement(searchQuery);
serPST.setString(1,textTitleSearch.getText());
ResultSet serResult = serPST.executeQuery();
int noofbooks = 0;
while(serResult.next()){
noofbooks++;
}
if (noofbooks!=0){
textResultTitle.setText(serResult.getString("BookTitle"));
textResultISBN.setText(Integer.toString((serResult.getInt("ISBN"))));
textResultPrice.setText(Integer.toString((serResult.getInt("Price"))));
textAvailability.setText(serResult.getString("IsAvailable"));
}
else{
JOptionPane.showMessageDialog(null,"Adjust the title");
}
serPST.close();
serResult.close();
}
catch(Exception errSearch){
JOptionPane.showMessageDialog(null, errSearch);
System.out.println(errSearch.getCause());

}
}

最佳答案

在 while 循环中,您将光标移动到最后一行,然后在 while 循环之后,您从没有记录的光标中获取结果?

您的代码应该是:

while(serResult.next()){
noofbooks++;
textResultTitle.setText(serResult.getString("BookTitle"));
textResultISBN.setText(Integer.toString((serResult.getInt("ISBN"))));
textResultPrice.setText(Integer.toString((serResult.getInt("Price"))));
textAvailability.setText(serResult.getString("IsAvailable"));
}
if (noofbooks == 0){
JOptionPane.showMessageDialog(null,"Adjust the title");
}

关于java - 无法从 sqlite 表获取记录,出现 ResultSet Closed 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com