为什么 While (rs.next()) 语句在第一次迭代后结束? [英] Why is While (rs.next()) statement ending after 1st iteration?

查看:29
本文介绍了为什么 While (rs.next()) 语句在第一次迭代后结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SELECT 语句从表中获取数据,然后将其插入到另一个表中.然而,stmt.executeQuery(query);"这一行正在插入表中的第一行然后退出.当我注释掉这一行时,while 循环会遍历所有打印出来的行.堆栈跟踪没有显示任何错误.为什么会发生这种情况?

尝试{String query = "SELECT * FROM "+schema_name+"."+table;rs = stmt.executeQuery(查询);而(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","");查询 = "插入 INDUCTION_INFO (BUNDLE, PROJECT_CD, DROPPER, WEEK, DROP_DT) "+ "值 ("+ 捆绑 +","+ "'"+project_cd+"',"+ 滴管+","+ 周+","+ "to_date('"+drop_dt+"','YYYY-MM-DD'))";System.out.println(查询);stmt.executeQuery(查询);}}catch(异常e){e.printStackTrace();}

解决方案

您正在重复使用 Statement,该Statement 用于在最后一行生成 rs循环.

这将关闭 ResultSet rs.正如文档中所述:><块引用>

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

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

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();
}

解决方案

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.

这篇关于为什么 While (rs.next()) 语句在第一次迭代后结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆