prepareStatement和Statement执行批处理的执行情况

preparestatement因为有预编译机制,每次执行相同sql的预编译,只会执行一次,下次只要设置参数就行,

适合相同sql的批处理

如果一定要多次编译不同sql,执行批处理的话,只会执行一个sql

public static void main(String[] args) throws Exception {
		ConnManager.initCurrPool();
		Connection dbConn = ConnManager.getConnByCurr();
		dbConn.setAutoCommit(false);
		PreparedStatement pstm = null;
		pstm = dbConn
				.prepareStatement(" update tc_day_summary set fund_head = 1 where work_day = '20180319' ");
		pstm.addBatch();
		pstm = dbConn
				.prepareStatement(" update take_order_addr set cust_name ='项羽' where aip_no = 'AIP100000721' ");
		pstm.addBatch();
		int[] executeBatch = pstm.executeBatch();
		System.out.println(executeBatch.length);
		// dbConn.commit();
	}
输出结果:1

如果需要多个不同的sql来执行批处理使用statement,上面的改为:

public static void main(String[] args) throws Exception {
		ConnManager.initCurrPool();
		Connection dbConn = ConnManager.getConnByCurr();
		dbConn.setAutoCommit(false);
		Statement stmt = null;
		stmt = dbConn.createStatement();
		stmt.addBatch("update tc_day_summary set fund_head = 1 where work_day = '20180319' ");
		stmt.addBatch(" update take_order_addr set cust_name ='项羽' where aip_no = 'AIP100000721' ");
		int[] executeBatch = stmt.executeBatch();
		System.out.println(executeBatch.length);
		// dbConn.commit();
	}
输出结果: 2



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在使用BasePeer.executeStatement()方法连续执行同一条数据的修改时,你需要注意以下几点: 1. 确保在每次执行之前,已经关闭或提交了上一次执行的结果。这可以通过调用BasePeer.doFinally()方法来实现。这样可以确保每次执行都是在一个干净的状态下进行。 2. 如果你需要连续执行相同的修改语句,可以考虑使用批处理操作。批处理操作是指将多个修改操作一起提交,以提高性能和效率。你可以使用BasePeer.doTransaction()方法在一个事务中执行多个修改操作。 下面是一个示例代码片段,展示了如何连续执行同一条数据的修改: ``` try { BasePeer.doTransaction(connection -> { for (int i = 0; i < numIterations; i++) { // 构造你的修改语句 String updateQuery = "UPDATE your_table SET your_column = ? WHERE id = ?"; // 创建PreparedStatement对象并设置参数 try (PreparedStatement statement = connection.prepareStatement(updateQuery)) { statement.setString(1, newValue); statement.setInt(2, id); // 执行修改语句 statement.executeUpdate(); } } // 提交事务 connection.commit(); }); } catch (SQLException e) { // 处理异常 e.printStackTrace(); } ``` 请根据你的具体需求修改代码中的表名、列名、参数等信息,并根据你的数据库连接方式进行适当的调整。 这样,你就可以使用BasePeer.executeStatement()方法连续执行同一条数据的修改了。记得根据实际情况进行错误处理和事务管理。希望对你有帮助!如果你有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值