java jdbc 批量查询_java – 使用不同的sql查询批量preparestatement

我发现现有问题

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();

}

}

}

但是,我正在寻找一种在不同的sql查询上执行批处理事务的方法.即插入表A并插入表B而没有SQL注入攻击的风险.我知道准备语句是避免此类攻击的首选方法,但我不知道在区分SQL查询时进行批处理事务的方法是什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值