简体   繁体   English

MYSQL插入查询未将数据更新到JAVA中的表

[英]MYSQL Insert query not updating data to the table in JAVA

I am working on a JAVA program which need to update database from text files. 我正在研究需要从文本文件更新数据库的JAVA程序。 I have successfully inserted and updated data. 我已成功插入和更新数据。 But i am facing a problem with here, this method. 但是我在这里遇到这种方法的问题。 Query runs without error and giving me the response. 查询运行没有错误,并给了我响应。 But the database table is not updating. 但是数据库表未更新。

private void filteData() {
        System.out.println("filteData");
        Statement statementAtenLogInsert = null; 
        Statement statementqCheck = null; 
        Statement statementUpdateProLog = null; 
        Statement statementEnterError = null; 
        ResultSet rs = null;
        int rcount;

        //Update successfull attendance_test
        String attenLogInsertSuccess = "INSERT INTO attendance_log (user_id, check_in, check_out) SELECT user_id, check_in, check_out FROM process_log WHERE flag = 'S'";

        try {
            statementAtenLogInsert = connection.createStatement();
            statementAtenLogInsert.execute(attenLogInsertSuccess);
            int qSuccess = statementAtenLogInsert.executeUpdate(attenLogInsertSuccess);

            System.out.println("qSuccess " + qSuccess);

            if(qSuccess > 0){

                String deleteProcessLog = "DELETE FROM process_log WHERE flag = 'S'";
                statementAtenLogInsert.execute(deleteProcessLog);

            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

Here the attenLogInsertSuccess and deleteProcessLog queries are not working. 这里的attenLogInsertSuccessdeleteProcessLog查询不起作用。 Mean nothing happened from database table side. 意味着从数据库表侧什么也没发生。 But qSuccess giving me a value. 但是qSuccess给了我一个价值。 That means attenLogInsertSuccess is triggering. 这意味着attenLogInsertSuccess正在触发。 But nothing happened from mysql side. 但是从mysql方面没有任何反应。

You need to close your connection in order to flush the changes to the database. 您需要关闭连接才能将更改刷新到数据库。

Try adding connection.close(); 尝试添加connection.close(); somewhere in your pipeline, typically you close the connection in a finally block to ensure it is always closed but it appears you have defined your connection elsewhere, presumably for re-use in the calling function. 通常,您可以在管道中的某个位置关闭finally块中的连接,以确保始终关闭该连接,但是您似乎已在其他位置定义了连接,可能是在调用函数中重用了。

You also need to close your statements before closing the connection. 在关闭连接之前,您还需要关闭语句。 See this similar answer for the pattern. 有关模式,请参见类似的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM