在循环中单独添加字符串时SQL查询出错

时间:2016-11-26 11:07:22

标签: java mysql sql-server jsp

当我在Servlet中循环查询时,我得到了锁定超时,所以我决定创建一个只执行一次的长查询。

以下是代码:

public void deactivateAccount(String[] userList) {
    DBConnectionFactory myFactory = DBConnectionFactory.getInstance();
    Connection con = myFactory.getConnection();

    try {
        con.setAutoCommit(false);

        String combinedQueries = "";


        for (int i =0; i<userList.length;i++) {
            String query = "UPDATE users SET active='0' WHERE userID = ? ; ";
            combinedQueries += query;
        }

        System.out.println(combinedQueries);

        PreparedStatement p = con.prepareStatement(combinedQueries);

        for (int i =0; i<userList.length;i++) {
            int currentNum = i+1;
            p.setInt(currentNum, Integer.parseInt(userList[i]));

            System.out.println("current num is " + currentNum);
            System.out.println("userlist value is " + Integer.parseInt(userList[i]));

        }


            p.execute();



    } catch (SQLException ex) {
        try {
            con.rollback();
        } catch (SQLException ex1) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex1);
        }
        Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}

这段代码在我的DAO中 - 它的作用是根据需要循环字符串多次(用户检查多少个复选框)然后在另一个循环中准备语句(设置准备语句的参数)然后执行它。

我收到错误:

  

严重:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在'UPDATE用户附近使用正确的语法SET active ='0'WHERE userID = 1; UPDATE用户SET active ='0'WHERE'在第1行       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

但是当我在执行代码之前在代码中打印查询时会出现:

  

信息:UPDATE用户SET active ='0'WHERERE userID =?; UPDATE用户SET active ='0'WHERERE userID =?; UPDATE用户SET active ='0'WHERERE userID =?; UPDATE用户SET active ='0'WHERERE userID =?;   (假设点击了4个复选框)

该方法接受来自servlet的request.getParameterValues的数组。

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

最好不要单独运行PreparedStatement execute()批量更新(出于性能原因),而是需要使用executeBatch(),如下所示:

public void deactivateAccount(String[] userList) {

    DBConnectionFactory myFactory = DBConnectionFactory.getInstance();
    Connection con = myFactory.getConnection();
    PreparedStatement p = null;

    try {
        con.setAutoCommit(false);
        String query = "UPDATE users SET active='0' WHERE userID = ?";
        p = con.prepareStatement(query);

        for (int i =0; i<userList.length;i++) {
            p.setInt(1, Integer.parseInt(userList[i]));
            p.addBatch();
        }
        int[] affectedRecords = p.executeBatch();
        con.commit();
        //you can validate rows before commit, upto your requirement
    } catch (SQLException ex) {
        try {
            con.rollback();
        } catch (SQLException ex1) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex1);
        }
        Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
            if(p != null)
              p.close();
            if(con != null)
              con.close();
    }
}

有关批量JDBC操作的更多详细信息,请参阅here