use*_*043 1 java sql exception jdbc
我正在研究一些抛出Operation not allowed after ResultSet closed异常的代码.这里是:
ResultSet res = stmt.executeQuery("SELECT codigo FROM projecto WHERE nome='"
+ auxiliarNomes.elementAt(i).toString() + "'");
while (res.next()) {
codigo = res.getString("codigo");
stmt.executeUpdate("insert into categoriaprojectos values("
+ "'" + codigo + "'" + "," + "'" + Antena.RetornaCodigoProjecto() + "')");
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
看起来像是获取java.sql.SQLException的副本:在ResultSet关闭后不允许操作.简而言之,您不能同时迭代ResultSet一些Statement并Statement在同一时间执行更新.
小智 6
这个有可能.只需使用额外的连接和声明.
Statement statementOne = connectionOne.createStatement();
Statement statementTwo = connectionTwo.createStatement();
ResultSet resultSetOne = statementOne.executeQuery("select * from x");
while (resultSetOne.next()) {
ResultSet resultSetTwo = statementTwo.executeQuery(String.format("select * from y where xy = %s", resultSetOne.getString(0)));
while (resultSetTwo.next()) {
String result = resultSetTwo.getString(0);
}
}
Run Code Online (Sandbox Code Playgroud)