PreparedStatement批量处理和事务代码
来源:互联网 发布:淘宝 论文代发 卓越 编辑:程序博客网 时间:2024/05/16 05:45
PreparedStatement批量处理和事务代码如下: /* * PreparedStatement: 1.addBatch() 将一组参数添加到 PreparedStatement对象内部 2.executeBatch() 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 * */ public class PreparedStatementCommitAndRollback { public static void main(String args[]) { Connection con = null; PreparedStatement pstm = null; try { // 1. 建立与数据库的连接 con = JDBCUtil.getConnection(); // 2. 执行sql语句 // 1).先创建PreparedStatement语句(发送slq请求): pstm = con.prepareStatement("insert into student values(?,?,?,?)"); con.setAutoCommit(false);//1,首先把Auto commit设置为false,不让它自动提交 // 2) 设置sql语句1 pstm.setInt(1, 33); pstm.setString(2,"wangqin"); pstm.setString(3, "c++"); pstm.setDouble(4, 78.5); // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 pstm.addBatch(); // 2) 设置sql语句2 pstm.setInt(1, 34); pstm.setString(2,"wuytun"); pstm.setString(3, "c"); pstm.setDouble(4, 77); // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 pstm.addBatch(); // 2) 设置sql语句3 pstm.setInt(1, 31); pstm.setString(2,"tetet"); pstm.setString(3, "c++"); pstm.setDouble(4, 90); // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 pstm.addBatch(); // 2) 设置sql语句4 pstm.setInt(1, 32); pstm.setString(2,"liug"); pstm.setString(3, "c"); pstm.setDouble(4, 50); // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 pstm.addBatch(); // 4) 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 pstm.executeBatch(); System.out.println("插入成功!"); // 若成功执行完所有的插入操作,则正常结束 con.commit();//2,进行手动提交(commit) System.out.println("提交成功!"); con.setAutoCommit(true);//3,提交完成后回复现场将Auto commit,还原为true, } catch (SQLException e) { try { // 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态 if(!con.isClosed()){ con.rollback();//4,当异常发生执行catch中SQLException时,记得要rollback(回滚); System.out.println("插入失败,回滚!"); con.setAutoCommit(true); } } catch (SQLException e1) { e1.printStackTrace(); } }finally{ JDBCUtil.closePreparedStatement(pstm); JDBCUtil.closeConnection(con); } } }
这是Statement的代码,同上:stm = con.createStatement(); con.setAutoCommit(false); // 若不出现异常,则继续执行到try语句完,否则跳转到catch语句中 stm.addBatch("insert into student values(23,'tangbao','高数',100)"); stm.addBatch("insert into student values(24,'王定','c#',98)"); stm.addBatch("insert into student values(25,'王国云','java',90)"); stm.addBatch("insert into student values(26,'溜出','英语',89)"); stm.addBatch("insert into student values(27,'wqde','java',63)"); /* * int[] executeBatch() throws * SQLException将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 */ stm.executeBatch();
1 0
- PreparedStatement批量处理和事务代码
- PreparedStatement批量处理和事务代码
- Statement 和PreparedStatement 批量处理JDBC语句提高处理速度
- JDBC的事务控制和批量处理
- JDBC的事务控制和批量处理
- JDBC的事务控制和批量处理
- Hibernate批量处理、事务和锁
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- JDBC的事务控制和批量处理.doc
- [转]Statement和PreparedStatement批量更新
- eclispe不出log
- 人脸检测——HyperFace
- web局部打印、去页眉页尾、兼容IE、Firefox、Chrome
- redisUtils
- C++ 经典面试题
- PreparedStatement批量处理和事务代码
- IllegalStateException :the specified child already has a parent
- 【Unity Shader入门精要】数学基础 — 点和矢量
- jsp1.0传统自定义标签详解
- 透明状态栏
- logback 常用配置详解(三) <filter>
- 【Unity Shader】一个简单的着色器
- iOS Build Fail 但是却没有红色警告提示!
- spark checkpoint