gpt4 book ai didi

java - JDBC : Operation not allowed after ResultSet closed

转载 作者:行者123 更新时间:2023-11-29 17:39:35 25 4
gpt4 key购买 nike

单击按钮时此代码显示此错误消息:

Operation not allowed after resultset closed

代码:

case 4:
BufferedReader choosest=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter your id");
int idst=Integer.parseInt(choosest.readLine());
String SQL = "SELECT * FROM student";
ResultSet ps = stmt.executeQuery(SQL);
while (ps.next()) {
int iddb = ps.getInt("id");
if(idst==iddb) {
BufferedReader lessonuser = new BufferedReader(new InputStreamReader(System.in));
System.out.print("choose lesson ");
String lest =lessonuser .readLine();
String SQL1 = "SELECT * FROM lesson";
ResultSet qs = stmt.executeQuery(SQL1);
while (qs.next()) {
String ledb = ps.getString("namel");
if (lest == ledb) {
String insertTableSQL1 ="INSERT INTO chooselesson (id, lesson) VALUES ("+idst+",'"+lest+"')";
stmt.executeUpdate(insertTableSQL1);
System.out.print("your unit sucssesfully add ");

}
}

}
}

最佳答案

当您进行另一个查询时,

ResultSet 将关闭。对于此代码,您可以首先收集ps的所有数据。然后迭代结果以执行其他查询。

我不得不说在循环中进行查询是很糟糕的性能操作。有很多方法可以避免这种情况。例如,您可以在一个查询中获取所有学生记录以及所有类(class)记录。然后使用lessonuser找出应插入chooselesson表中的记录,并以批处理模式运行insert SQL。

关于java - JDBC : Operation not allowed after ResultSet closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072950/

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