Java 为什么不能在while循环中执行updateSql

Java 为什么不能在while循环中执行updateSql,java,sql,oracle,mybatis,Java,Sql,Oracle,Mybatis,当我有一个选择时,它可以工作: public static void main(String[] args){ try{ Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw"); Statement stm = conn.createStatement(); String selectSql="sel

当我有一个选择时,它可以工作:

public static void main(String[] args){

    try{
            Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
            Statement stm = conn.createStatement();
            String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";

            ResultSet ress = stm.executeQuery(selectSql);
            while(ress.next()){
                int id = ress.getInt("id");
                String user_id = ress.getString("user_id");
                long start_time = ress.getLong("start_time");
                long end_time = ress.getLong("end_time");
                int start_date = ress.getInt("start_date");
                int end_date = ress.getInt("end_date");
                String time_zone = ress.getString("time_zone");
                System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
            }
        }catch(SQLException e){
        e.printStackTrace();
        }
    //Close the resources - Done automatically by try-with-resources
}
输出:

1测试用户20151025021324 20151026024135 0 0 GMT-12:00 2招待所20151026024135 20151022080352 0 GMT-07:00 24压力0101 20151208075641 20151208075717 0 0零零 88 123 c 20151224085208 20151224130803 0 0零零 89 123 c 20151224130916 20151224130922 0 0 0零 90 123 c 20151224130917 20151224130923 0 0 0零 91 123 c 20151224130918 20151224130924 0 0零 92 123 c 20151224130919 20151224130925 0 0 0零

但当我想在循环中更新时:

try{
        Connection conn = DriverManager.getConnection("jdbc:oracle:XXXXX", "username", "psw");
        Statement stm = conn.createStatement();
        String selectSql="select h.id, h.user_id,h.start_time,h.end_time,h.start_date,h.end_date, a.time_zone from hos_driving_log h left join user_info ua on h.user_id= ua.user_id left JOIN account a on a.acct_id = ua.acct_id";

        ResultSet ress = stm.executeQuery(selectSql);
        while(ress.next()){
            int id = ress.getInt("id");
            String user_id = ress.getString("user_id");
            long start_time = ress.getLong("start_time");
            long end_time = ress.getLong("end_time");
            int start_date = ress.getInt("start_date");
            int end_date = ress.getInt("end_date");
            String time_zone = ress.getString("time_zone");
            System.out.println(id+" "+user_id+" "+start_time+" "+end_time+" "+start_date+" "+end_date+" "+time_zone);
            start_date = getDrivingLogShortTypeDate(start_time, time_zone);
            end_date = getDrivingLogShortTypeDate(end_time, time_zone);
            String updateSql = "update hos_driving_log set start_date = "+ start_date+",end_date = "+end_date+" where id="+id;
            int i = stm.executeUpdate(updateSql);
            System.out.println(updateSql);
        }
    }catch(SQLException e){
    e.printStackTrace();
    }
//Close the resources - Done automatically by try-with-resources
。。。它只执行一次更新:

45 co2-0096 20151222090816 20151222091436 20151222 20151222 GMT+08:00 更新hos_驾驶日志集开始日期=20151222,结束日期=20151222,其中id=45


为什么在一次更新后它会停止?如何获取要更新的所有行?

因为当生成ResultSet对象的语句对象关闭、重新执行或用于从多个结果序列中检索下一个结果时,ResultSet对象将自动关闭

您正在重新执行生成结果集的语句对象stm。您需要为更新使用单独的语句对象


另外,不要通过将文本连接到SQL中来构建SQL更新语句。使用绑定变量,您的用户和DBA都会感谢您


请看

请在格式化您的帖子方面投入一些精力。如果您不知道如何使用UI,帮助中心可以帮助您。它似乎是完全不同的数据,因为45不在第一个输出中。数据改变了吗?或者您正在连接到其他数据库或架构?@jolie您的意思是,在添加update语句后,print语句只打印一次?你查过数据库了吗?为什么在这个问题上都投了反对票?这是一个公平的问题,回答这个问题所需的所有信息都已提供。@jolie虽然您在代码中的注释中说要使用try with resources,但您没有。最好使用它们或使用finally块关闭资源。另外,不要通过将文本连接到SQL中来构建SQL更新语句。使用绑定变量,您的用户和DBA都会感谢您。-------这是什么意思?基本上,每次更新都会重复使用相同的SQL。如果您执行1000次更新,那么这就是解析1000次并缓存1000条语句与解析并仅缓存一条语句之间的区别。看见