我尝试从数据库中获取一条数据,突然它给出了错误:ResultSet 关闭后不允许操作。
while(rs3.next()) {
为什么会这样?代码似乎没问题,一切都很好。这是我的代码:
public DatabaseConnectionClass() {
idfield = new JTextField("");
submit = new JButton("go");
settings = new JButton("settings");
auto = new JButton("auto");
add(idfield);
add(submit);
add(settings);
add(auto);
setLayout(new GridLayout());
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database1?useTimezone=true&serverTimezone=UTC", "root", "");
st = con.createStatement();
con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/values?useTimezone=true&serverTimezone=UTC", "root", "");
st2 = con2.createStatement();
} catch(Exception ex) {
System.out.println(ex);
}
}
这是在按下其中一个按钮(称为自动(时激活的错误方法:
public void getAuto() throws Exception {
for (int i = 0; i < 1000; i++) {
String q1 = "SELECT * FROM payments LIMIT 1";
try {
rs3=st.executeQuery(q1);
while(rs3.next()) {
String ppmail = rs3.getString("mail");
getData(ppmail);
rs3.close();
}} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
auto.setText("End of records");
}
}
}
关闭ResultSet
后,rs3.close();
移动到finally
块
}} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
auto.setText("End of records");
} finally {
if (s3 != null){
s3.close();
}
}
立即释放此结果集对象的数据库和 JDBC 资源