为什么 While (rs.next()) 语句在第一次迭代后结束?
我正在使用 SELECT 语句从表中获取数据,然后将其插入到另一个表中。然而,“stmt.executeQuery(query);”行正在插入表中的第一行然后退出。当我注释掉这一行时, while 循环会循环遍历所有打印出来的行。堆栈跟踪没有显示任何错误。为什么会发生这种情况?
try{
String query = "SELECT * FROM "+schema_name+"."+table;
rs = stmt.executeQuery(query);
while (rs.next()) {
String bundle = rs.getString("BUNDLE");
String project_cd = rs.getString("PROJECT_CD");
String dropper = rs.getString("DROPPER");
String week = rs.getString("WEEK");
String drop_dt = rs.getString("DROP_DT").replace(" 00:00:00.0","");
query = "INSERT INTO INDUCTION_INFO (BUNDLE, PROJECT_CD, DROPPER, WEEK, DROP_DT) "
+ "VALUES ("
+ bundle+","
+ "'"+project_cd+"',"
+ dropper+","
+ week+","
+ "to_date('"+drop_dt+"','YYYY-MM-DD'))";
System.out.println(query);
stmt.executeQuery(query);
}
}catch(Exception e){
e.printStackTrace();
}
I am using a SELECT statement to get data from a table and then insert it into another table. However the line "stmt.executeQuery(query);" is inserting the first line from the table then exits. When I comment this line out, the while loop loops through all the lines printing them out. The stacktrace isn't showing any errors. Why is this happening?
try{
String query = "SELECT * FROM "+schema_name+"."+table;
rs = stmt.executeQuery(query);
while (rs.next()) {
String bundle = rs.getString("BUNDLE");
String project_cd = rs.getString("PROJECT_CD");
String dropper = rs.getString("DROPPER");
String week = rs.getString("WEEK");
String drop_dt = rs.getString("DROP_DT").replace(" 00:00:00.0","");
query = "INSERT INTO INDUCTION_INFO (BUNDLE, PROJECT_CD, DROPPER, WEEK, DROP_DT) "
+ "VALUES ("
+ bundle+","
+ "'"+project_cd+"',"
+ dropper+","
+ week+","
+ "to_date('"+drop_dt+"','YYYY-MM-DD'))";
System.out.println(query);
stmt.executeQuery(query);
}
}catch(Exception e){
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在重复使用用于在循环最后一行生成
rs
的Statement
。这将关闭
ResultSet
rs
。正如文档中所述:您需要使用第二个
Statement
对象来执行INSERT
语句。You are re-using the
Statement
that was used to producers
on the last line of your loop.This will close the
ResultSet
rs
. As stated in the documentation:You need to use a second
Statement
object to execute theINSERT
statements.Statement
对象一次只能做一件事,因此当您执行该INSERT
时,您会使其生成的ResultSet
失效。您需要创建第二个Statement
对象来执行INSERT
。来自 Statement 文档:“默认情况下,每个对象只有一个 ResultSet 对象Statement 对象可以同时打开。因此,如果一个 ResultSet 对象的读取与另一个 ResultSet 对象的读取交错,则每个 ResultSet 对象都必须由不同的 Statement 对象生成。 Statement 接口中的所有执行方法都会隐式关闭语句的当前 ResultSet。对象如果是开放的存在。”
Statement
objects can only do one thing at a time, so when you execute thatINSERT
, you invalidate theResultSet
which it generated. You'll need to create a secondStatement
object to perform theINSERT
.From the Statement documentation: "By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists."
如果使用相同的语句,将使之前的结果集失效。您应该使用不同的语句来执行更新/插入。
if you use the same statement, it will invalidate the previous result set. You should use a different statement to perform updates/inserts.
这是来自接口 Statement 的 Java 文档:
因此,您最好使用第二个
Statement
,甚至更好的是PreparedStatement
。要执行 INSERT SQL 语句,您应该使用
executeUpdate()
而不是executeQuery()
。This is from the Java docs of interface Statement:
So you better use a second
Statement
or even better aPreparedStatement
.And to execute an INSERT SQL statement you should use
executeUpdate()
instead ofexecuteQuery()
.