gpt4 book ai didi

java.sql.SQLException : error occurred during batching: batch must be either executed or cleared 异常

转载 作者:行者123 更新时间:2023-11-29 05:58:43 29 4
gpt4 key购买 nike

我得到了

java.sql.SQLException: error occurred during batching: batch must be either executed or cleared

在下面的代码中。我想执行两个查询。

String sql_query="insert into m_status values(20,'test');select * from m_user";
String query1 = sql_query.toUpperCase();

String[] results = query1.split(";");

stmt = conn1.createStatement();
conn1.setAutoCommit(false);
for (int i = 0; i < results.length; i++) {
if(results[i].startsWith("SELECT")) {
rs1 = stmt.executeQuery(results[i]);
}
else if(results[i].startsWith("INSERT")){
stmt.addBatch(results[i]);
}
}
int [] updateCounts = stmt.executeBatch();
conn1.commit();
if (rs1 != null)
rs1.close();
if (stmt != null)
stmt.close();
if (conn1 != null)
conn1.close();

最佳答案

使用两个语句,一个用于选择查询,另一个用于更新。 executeQuery 检查是否有针对该 Statament 的批处理,如果有则抛出该错误。此外,将查询包装在 try-catch-finally block 中并关闭 finally block 中的资源。实际上,您的代码可能会导致连接泄漏。

关于java.sql.SQLException : error occurred during batching: batch must be either executed or cleared 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030081/

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