为什么 While (rs.next()) 语句在第一次迭代后结束?

发布于 2024-11-28 09:06:44 字数 1004 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光飞翔i 2024-12-05 09:06:45

您正在重复使用用于在循环最后一行生成 rsStatement

这将关闭 ResultSet rs。正如文档中所述

当生成该对象的 Statement 对象被关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet 对象会自动关闭.

您需要使用第二个 Statement 对象来执行 INSERT 语句。

You are re-using the Statement that was used to produce rs on the last line of your loop.

This will close the ResultSet rs. As stated in the documentation:

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

You need to use a second Statement object to execute the INSERT statements.

谈下烟灰 2024-12-05 09:06:45

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 that INSERT, you invalidate the ResultSet which it generated. You'll need to create a second Statement object to perform the INSERT.

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."

樱花落人离去 2024-12-05 09:06:45

如果使用相同的语句,将使之前的结果集失效。您应该使用不同的语句来执行更新/插入。

if you use the same statement, it will invalidate the previous result set. You should use a different statement to perform updates/inserts.

初心 2024-12-05 09:06:45

这是来自接口 Statement 的 Java 文档:

默认情况下,每个Statement对象只能打开一个ResultSet对象
同时。

因此,您最好使用第二个Statement,甚至更好的是PreparedStatement

要执行 INSERT SQL 语句,您应该使用 executeUpdate() 而不是 executeQuery()

This is from the Java docs of interface Statement:

By default, only one ResultSet object per Statement object can be open
at the same time.

So you better use a second Statement or even better a PreparedStatement.

And to execute an INSERT SQL statement you should use executeUpdate() instead of executeQuery().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文