执行批处理不更新 SQL 表中的数据 -

问题描述

我正在尝试使用执行批处理更新表。我没有收到任何错误。程序正在成功执行,但是表没有更新数据。我不确定是什么阻止了 sql 表中的更新。请帮忙..

    PreparedStatement pstmtAssignmentTable = null;
    try {
        Connection connection = DriverManager.getConnection(url);
        try {
            connection.setAutoCommit(false);
            try {

            String sql_SelectSpecialSPOCInfo = "Select CustomerNumber,U.UserId,U.TeamId,SPOC.EmployeeId,WorkProfileId from admn.SpecialSPOCAssignmentLookUp SPOC join admn.Users U on SPOC.EmployeeID = U.EmployeeID join admn.UserPhone UP on UP.UserID = U.UserID join admn.SpecialCustomerLookUp LLU on LLU.CustomerCode = SPOC.CustomerCode";
            String sql_updateAssignmentTable = "Update asgn.Assignment Set UserId = ?,TeamId = ?,Source = 'Manual Assignment',WorkProfileId = ?,AssignedDate = cast(getdate() as date)  where CustomerNumber = ?";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql_SelectSpecialSPOCInfo);
            ManualAssignmentModel employeeInfo = new ManualAssignmentModel();
            int recordNo = 0;
            int batchSize = 100;
            while (rs.next()) {
                employeeInfo.setCustomerNumber(rs.getString("CustomerNumber"));
                employeeInfo.setUserId(rs.getString("UserId"));
                employeeInfo.setTeamId(rs.getString("TeamId"));
                employeeInfo.setWorkProfileId(rs.getString("WorkProfileId"));
                recordNo++;
                pstmtAssignmentTable = connection.prepareStatement(sql_updateAssignmentTable);
                {
                    pstmtAssignmentTable.setString(1,employeeInfo.getUserId());
                    pstmtAssignmentTable.setString(2,employeeInfo.getTeamId());
                    pstmtAssignmentTable.setString(3,employeeInfo.getWorkProfileId());
                    pstmtAssignmentTable.setString(4,employeeInfo.getCustomerNumber());
                    System.out.println("Adding to Batch ");
                    pstmtAssignmentTable.addBatch();
                }
                if (recordNo == batchSize) {
                    System.out.println(
                            "Batch Limit Reached. Updating the Database with the items added in the current Batch");    
                    pstmtAssignmentTable.executeBatch();
                    System.out.println("Batch execute cpmpleted..");
                    pstmtAssignmentTable.clearBatch();
                    recordNo = 0;
                }

            }
             System.out.println("Record count is less than the Batch Limit. Updating the
             Database with available records...");
             pstmtAssignmentTable.executeBatch();
             System.out.println(" Job Completed");

            } finally {
            if (pstmtAssignmentTable != null) {
                pstmtAssignmentTable.close();
            }
        }
             connection.commit();

        } catch (Exception e) {
            connection.rollback();
        }
    } catch (Exception e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }

解决方法

这里有很多可能的原因:

  1. 也许 select 查询返回少于 100 行(int batch=100),所以你永远不会输入 if 语句......即使我们假设有超过 100 行,你的代码也不会执行最后100个元素之后的元素(363行-> 300行会更新,最后63行代码不会进入if语句):

    if (recordNo == batchSize) { //代码 }

  2. 在你的while语句中,你再次分配pstmtAssignmentTable,你需要把它移到while之外:

    pstmtAssignmentTable = connection.prepareStatement(sql_updateAssignmentTable); 而(rs.next()){ //代码 }

修正:

这是经过上述修改的代码:

    PreparedStatement pstmtAssignmentTable = null;
    try {
        Connection connection = DriverManager.getConnection(url);
        try {
            connection.setAutoCommit(false);
            try {
                String sql_SelectSpecialSPOCInfo = "Select CustomerNumber,U.UserId,U.TeamId,SPOC.EmployeeId,WorkProfileId from admn.SpecialSPOCAssignmentLookUp SPOC join admn.Users U on SPOC.EmployeeID = U.EmployeeID join admn.UserPhone UP on UP.UserID = U.UserID join admn.SpecialCustomerLookUp LLU on LLU.CustomerCode = SPOC.CustomerCode";
                String sql_updateAssignmentTable = "Update asgn.Assignment Set UserId = ?,TeamId = ?,Source = 'Manual Assignment',WorkProfileId = ?,AssignedDate = cast(getdate() as date)  where CustomerNumber = ?";

                //
                String sql_CountSpecialSPOCInfo = "Select COUNT(*) from admn.SpecialSPOCAssignmentLookUp SPOC join admn.Users U on SPOC.EmployeeID = U.EmployeeID join admn.UserPhone UP on UP.UserID = U.UserID join admn.SpecialCustomerLookUp LLU on LLU.CustomerCode = SPOC.CustomerCode";
                ResultSet rsCount = connection.createStatement().executeQuery(sql_CountSpecialSPOCInfo);
                //BE CAREFUL: In Case this throws an exception,maybe we need to use  rsCount.next(); to move to the first row
                int size = rsCount.getInt(1);
                //If the size is less than the batchSize,then we use the size to
                int batchSize = size > 100 ? 100 : size;


                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(sql_SelectSpecialSPOCInfo);
                ManualAssignmentModel employeeInfo = new ManualAssignmentModel();
                int recordNo = 0;


                pstmtAssignmentTable = connection.prepareStatement(sql_updateAssignmentTable);
                while (rs.next()) {
                    employeeInfo.setCustomerNumber(rs.getString("CustomerNumber"));
                    employeeInfo.setUserId(rs.getString("UserId"));
                    employeeInfo.setTeamId(rs.getString("TeamId"));
                    employeeInfo.setWorkProfileId(rs.getString("WorkProfileId"));
                    recordNo++;

                    pstmtAssignmentTable.setString(1,employeeInfo.getUserId());
                    pstmtAssignmentTable.setString(2,employeeInfo.getTeamId());
                    pstmtAssignmentTable.setString(3,employeeInfo.getWorkProfileId());
                    pstmtAssignmentTable.setString(4,employeeInfo.getCustomerNumber());
                    System.out.println("Adding to Batch ");
                    pstmtAssignmentTable.addBatch();

                    if (recordNo == batchSize) {
                        System.out.println("Batch Limit Reached. Updating the Database with the items added in the current Batch");
                        pstmtAssignmentTable.executeBatch();
                        System.out.println("Batch execute cpmpleted..");
                        pstmtAssignmentTable.clearBatch();
                        recordNo = 0;
                    }

                }

                // We will still need to excuteBatch in case recordNo<batchSize
                if(recordNo>0){
                    System.out.println("Updating the Database with the items added in the current Batch");
                    pstmtAssignmentTable.executeBatch();
                    System.out.println("Batch execute cpmpleted..");
                    pstmtAssignmentTable.clearBatch();
                }

                System.out.println("Record count is less than the Batch Limit. Updating the Database with available records...");
                pstmtAssignmentTable.executeBatch();
                System.out.println(" Job Completed");

            } finally {
                if (pstmtAssignmentTable != null) {
                    pstmtAssignmentTable.close();
                }
            }
            connection.commit();

        } catch (Exception e) {
            connection.rollback();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

请分享结果,以便我们更新答案并使其更通用。