批量preparedstatement与不同的SQL查询

时间:2015-12-02 11:18:01

标签: java jdbc prepared-statement sql-injection batch-processing

我发现现有问题similar对此问题实际上没有明确答案。

使用一个SQL查询的正常批处理预备语句如下所示:

private static void batchInsertRecordsIntoTable() throws SQLException {

        Connection dbConnection = null;
        PreparedStatement preparedStatement = null;

        String insertTableSQL = "INSERT INTO DBUSER"
                + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
                + "(?,?,?,?)";

        try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(insertTableSQL);

            dbConnection.setAutoCommit(false);

            preparedStatement.setInt(1, 101);
            preparedStatement.setString(2, "mkyong101");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 102);
            preparedStatement.setString(2, "mkyong102");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.setInt(1, 103);
            preparedStatement.setString(2, "mkyong103");
            preparedStatement.setString(3, "system");
            preparedStatement.setTimestamp(4, getCurrentTimeStamp());
            preparedStatement.addBatch();

            preparedStatement.executeBatch();

            dbConnection.commit();

            System.out.println("Record is inserted into DBUSER table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());
            dbConnection.rollback();

        } finally {

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

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

        }

    }

取自:http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/

但是,我正在寻找一种方法来对不同的 sql查询执行批处理事务。即INSERT INTO TABLE AINSERT INTO TABLE B,没有SQL注入攻击的风险。我知道准备语句是避免此类攻击的首选方法,但我不知道如何在区分SQL查询时进行批处理事务?

1 个答案:

答案 0 :(得分:8)

对于两(2)个不同的SQL查询,您将需要两(2)个不同的PreparedStatement对象,每个对象都有自己的批处理,但是当您想要将查询发送到服务器:

try (
        PreparedStatement thisPs = conn.prepareStatement("INSERT INTO thisTable (thisId, thisText) VALUES (?,?)");
        PreparedStatement thatPs = conn.prepareStatement("INSERT INTO thatTable (thatId, thatText) VALUES (?,?)")) {

    thisPs.setInt(1, 1);
    thisPs.setString(2, "thisText1");
    thisPs.addBatch();

    thatPs.setInt(1, 1);
    thatPs.setString(2, "thatText1");
    thatPs.addBatch();

    thisPs.setInt(1, 2);
    thisPs.setString(2, "thisText2");
    thisPs.addBatch();

    thatPs.setInt(1, 2);
    thatPs.setString(2, "thatText2");
    thatPs.addBatch();

    thisPs.executeBatch();
    thatPs.executeBatch();
}

另外,请注意术语。谈论"批量交易"有点含糊不清:

  • addBatchexecuteBatch是将多个语句作为单个批次(传输)发送到服务器的机制的一部分。这会影响语句发送(传输)到数据库服务器的方式。

  • 数据库事务是一种机制,可以将多个语句作为一个完整的组处理,即整个组将被处理(& #34;已提交")或整个组将被丢弃("回滚")。 Connection#setAutoCommit()Connection#commit()Connection#rollback()方法控制此行为。这会影响数据库服务器执行语句的方式。