?1.JDBC批处理
?
package com.yli.demo; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; /** * 简单批处理测试 * @author yli * */ public class BatchTest { public static void main(String[] args) { // Test1(); Test2(); } /** * 非事务测试<br> * 批处理操作不能包含查询SQL,否则会抛出异常<br> * 批处理返回的结果是整型数组,表示每一条SQL语句执行成功后受影响的记录条数 */ public static void Test1() { try { Connection conn; conn = ConnectionUtil.getConnection(); String sql = "update es_t_shop_affiche set affichetitle='admin' where afficheid=100001"; Statement stat = conn.createStatement(); stat.addBatch(sql); sql = "update es_t_shop_affiche set affichetitle='root' where afficheid=100002"; stat.addBatch(sql); int[] count = stat.executeBatch(); for (int i = 0; i < count.length; i++) { System.out.println(count[i]); } ConnectionUtil.close(conn); } catch (SQLException e) { e.printStackTrace(); } } /** * 批处理操作必须视为一个事务 */ public static void Test2() { try { Connection conn; conn = ConnectionUtil.getConnection(); boolean autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); String sql = "update es_t_shop_affiche set affichetitle='aaa' where afficheid=100001"; Statement stat = conn.createStatement(); stat.addBatch(sql); sql = "update es_t_shop_affiche set affichetitle='bbb' where afficheid=100002"; stat.addBatch(sql); int[] count = stat.executeBatch(); for (int i = 0; i < count.length; i++) { System.out.println(count[i]); } conn.commit(); // 将是否自动提交属性还原 conn.setAutoCommit(autoCommit); ConnectionUtil.close(conn); } catch (SQLException e) { e.printStackTrace(); } } }
?
?
?