PreparedStatement BatchUpdate仅执行批处理中的最后一个准备好的语句

时间:2018-08-23 22:01:41

标签: java sql-server prepared-statement batch-updates

我具有以下方法来批量执行准备好的语句:

public void cleanupForSearchReplaceMap(Map<String, String> SearchReplaceMap) throws SQLException{

    System.out.println("we are insde the cleanupForSearchReplaceMap method");

    Connection conn = null;
    PreparedStatement pstmt = null;

    String sql3 = "UPDATE BODYCONTENT" +
         " SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT)" +
         " FROM dbo.BODYCONTENT" +
         " WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'";

    try {
        conn = DatabaseUtil.getConnection(); 

        System.out.println("connection established...");

        conn.setAutoCommit(false);

        // for(String str: searchStrList) {
        for (Map.Entry<String, String> entry : SearchReplaceMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            System.out.println(" query: " + sql3 + "   searchstring: " + key + "  replacestring: " + value);

            pstmt = conn.prepareStatement(sql3);

            pstmt.setNString(1, key.trim());

            System.out.println(" 1. settingstring " + key);

            pstmt.setNString(2, value.trim());

            System.out.println(" 2. settingstring " + value);
            pstmt.setNString(3, key.trim());

            System.out.println(" 2. settingstring " + key);

            System.out.println(" here is the prepared statement:    " + pstmt);

            pstmt.addBatch();
        }

        pstmt.executeBatch();

        System.out.println(" length of preparedstatement.executeBatch():  " + pstmt.executeBatch().length);

        conn.commit();
   } catch (SQLException e) {
      System.out.println(e.getMessage());
      //conn.rollback();
  } finally {
       if (pstmt != null) {
           pstmt.close();
        }

       if (conn != null) {
            conn.close();
        }
    }
}

stmt.executeBatch()的长度始终为0。我不知道为什么。

这是我在日志中看到的:

connection established...
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: a  replacestring: BMW
 1. settingstring a
 2. settingstring BMW
 2. settingstring a
 here is the prepared statement:    SQLServerPreparedStatement:1
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: confluence  replacestring: BMW
 1. settingstring confluence
 2. settingstring BMW
 2. settingstring confluence
 here is the prepared statement:    SQLServerPreparedStatement:2
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: import  replacestring: BMW
 1. settingstring import
 2. settingstring BMW
 2. settingstring import
 here is the prepared statement:    SQLServerPreparedStatement:3
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: technical  replacestring: BMW
 1. settingstring technical
 2. settingstring BMW
 2. settingstring technical
 here is the prepared statement:    SQLServerPreparedStatement:4
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: in  replacestring: BMW
 1. settingstring in
 2. settingstring BMW
 2. settingstring in
 here is the prepared statement:    SQLServerPreparedStatement:5
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: Eclipse  replacestring: BMW
 1. settingstring Eclipse
 2. settingstring BMW
 2. settingstring Eclipse
 here is the prepared statement:    SQLServerPreparedStatement:6
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: Piano  replacestring: BMW
 1. settingstring Piano
 2. settingstring BMW
 2. settingstring Piano
 here is the prepared statement:    SQLServerPreparedStatement:7
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: file  replacestring: BMW
 1. settingstring file
 2. settingstring BMW
 2. settingstring file
 here is the prepared statement:    SQLServerPreparedStatement:8
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: bubble  replacestring: BMW
 1. settingstring bubble
 2. settingstring BMW
 2. settingstring bubble
 here is the prepared statement:    SQLServerPreparedStatement:9
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: and  replacestring: BMW
 1. settingstring and
 2. settingstring BMW
 2. settingstring and
 here is the prepared statement:    SQLServerPreparedStatement:10
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: text  replacestring: BMW
 1. settingstring text
 2. settingstring BMW
 2. settingstring text
 here is the prepared statement:    SQLServerPreparedStatement:11
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: copy  replacestring: BMW
 1. settingstring copy
 2. settingstring BMW
 2. settingstring copy
 here is the prepared statement:    SQLServerPreparedStatement:12
 query: UPDATE BODYCONTENT SET BODY = CAST(REPLACE(CAST(BODY AS NVARCHAR(MAX)), ? , ?) AS NTEXT) FROM dbo.BODYCONTENT WHERE BODY LIKE '%[^A-Za-z0-9]' + ? + '[^A-Za-z0-9]%'   searchstring: page  replacestring: BMW
 1. settingstring page
 2. settingstring BMW
 2. settingstring page
 here is the prepared statement:    SQLServerPreparedStatement:13
 length of preparedstatement.executeBatch():  0

每次仅执行最后一个准备好的语句。

0 个答案:

没有答案