首页 > 解决方案 > for 循环中的 Microsoft SQL Server Prepared 语句不起作用

问题描述

我需要以下帮助。我正在尝试两种方式。两者都没有工作。没有更新行。相同的 sql 可以在没有循环的情况下工作(对于单个字符串)。但我想为多个字符串执行相同的 sql。

方法1:

       PreparedStatement pstmt = null;

       String query = "UPDATE PeopleTable SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), ? ,'ReplacedFirstName') as ntext)";

       pstmt = conn.prepareStatement(query);
       for(String s: myList) {

       pstmt.setString(1, s);

       pstmt.addBatch();

       }

       pstmt.executeUpdate();

方法:2:

     PreparedStatement pstmt = null;

     String query = "UPDATE PeopleTable SET firstname = CAST(REPLACE(CAST(firstname as nvarchar(max)), ? ,'ReplacedFirstName') as ntext)";

     pstmt = conn.prepareStatement(query);
     for(String s: myList) {

     pstmt.setString(1, s);

     pstmt.executeUpdate();
     stmt.clearParameters();

     }

两者都不起作用。请帮忙。

标签: javasql-serversql-updateprepared-statement

解决方案


推荐阅读