[转]Statement和PreparedStatement批量更新
来源:互联网 发布:佳能数码单反相机软件 编辑:程序博客网 时间:2024/06/04 18:32
PreparedStatement: 关于PreparedStatement插入Date类型值的方法java中的PreparedStatement类型的setDate() 方法要求 java.sql.Date,而 java.sql.Date 的构造函数不能为空。所以正确做法是:pstmt.setDate(8, new Date(System.currentTimeMillis()));或者:pstmt.setDate(8, new Date(new java.util.Date().getTime()));另外,setDate()只能是日期型,最小单位是日。假如需要时分秒需要:pstmt.setTiemstamp(8, new Timestamp(System.currentTimeMillis()));或者:pstmt.setTimestamp(8, new Timestamp(new java.util.Date().getTime()));如果把从自己文件中的String类型的Date直接存入数据库中,可以使用pstmt.setTimestamp(8,Timestamp.valueOf(in.get(8)) );源于:http://liudingfeng.iteye.com/blog/7328371) addBatch()将一组参数添加到PreparedStatement对象内部。2) executeBatch()将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。Statement:1) addBatch(String sql)方法会在批处理缓存中加入一条sql语句。2) executeBatch()执行批处理缓存中的所有sql语句。注意:PreparedStatement中使用批量更新时,要先设置好参数后再使用addBatch()方法加入缓存。批量更新中只能使用更改、删除或插入语句。 1Statement批量处理和事务代码如下: 2package com.ambow.day20.jdbc.JDBCTestCommitAndRollback; 3import java.sql.Connection; 4import java.sql.SQLException; 5import java.sql.Statement; 6import com.ambow.day19.jdbc.util.JDBCConAndClo; 7/* 8 *1,首先把Auto commit设置为false,不让它自动提交 9 *2,进行手动提交(commit) 10 *3,提交完成后回复现场将Auto commit,还原为true, 11 *4,当异常发生执行catch中SQLException时,记得要rollback(回滚); 12 * */ 13public class StatementCommitAndRollbackTest { 14 public static void main(String args[]) { 15 Connection con = null; 16 Statement stm = null; 17 try { 18 con = JDBCConAndClo.getConnectionBao(); 19 stm = con.createStatement(); 20 con.setAutoCommit(false); 21 // 若不出现异常,则继续执行到try语句完,否则跳转到catch语句中 22 stm.addBatch("insert into student values(23,'tangbao','高数',100)"); 23 stm.addBatch("insert into student values(24,'王定','c#',98)"); 24 stm.addBatch("insert into student values(25,'王国云','java',90)"); 25 stm.addBatch("insert into student values(26,'溜出','英语',89)"); 26 stm.addBatch("insert into student values(27,'wqde','java',63)"); 27 /* 28 * int[] executeBatch() throws 29 * SQLException将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 30 */ 31 stm.executeBatch(); 32 System.out.println("插入成功!"); 33 // commit:若成功执行完所有的插入操作,则正常结束 34 con.commit(); 35 System.out.println("提交成功!"); 36 con.setAutoCommit(true); 37 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 try { 41 //rollback: 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态 42 if (!con.isClosed()) { 43 con.rollback(); 44 System.out.println("提交失败,回滚!"); 45 con.setAutoCommit(true); 46 } 47 } catch (SQLException e1) { 48 e1.printStackTrace(); 49 } finally { 50 JDBCConAndClo.closeStatement(stm); 51 JDBCConAndClo.closeConnection(con); 52 } 53 } 54 } 55} 56PreparedStatement批量处理和事务代码如下: 57package com.ambow.day20.jdbc.JDBCTestCommitAndRollback; 58import java.sql.Connection; 59import java.sql.PreparedStatement; 60import java.sql.SQLException; 61import com.ambow.day19.jdbc.util.JDBCConAndClo; 62 63/* 64 * PreparedStatement: 65 1.addBatch() 将一组参数添加到 PreparedStatement对象内部 66 2.executeBatch() 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 67 * 68 */ 69public class PreparedStatementCommitAndRollbackTest { 70 public static void main(String args[]) { 71 Connection con = null; 72 PreparedStatement pstm = null; 73 74 try { 75 // 1. 建立与数据库的连接 76 con = JDBCConAndClo.getConnectionBao(); 77 // 2. 执行sql语句 78 // 1).先创建PreparedStatement语句(发送slq请求): 79 pstm = con.prepareStatement("insert into student values(?,?,?,?)"); 80 con.setAutoCommit(false);//1,首先把Auto commit设置为false,不让它自动提交 81 // 2) 设置sql语句1 82 pstm.setInt(1, 33); 83 pstm.setString(2,"wangqin"); 84 pstm.setString(3, "c++"); 85 pstm.setDouble(4, 78.5); 86 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 87 pstm.addBatch(); 88 // 2) 设置sql语句2 89 pstm.setInt(1, 34); 90 pstm.setString(2,"wuytun"); 91 pstm.setString(3, "c"); 92 pstm.setDouble(4, 77); 93 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 94 pstm.addBatch(); 95 // 2) 设置sql语句3 96 pstm.setInt(1, 31); 97 pstm.setString(2,"tetet"); 98 pstm.setString(3, "c++"); 99 pstm.setDouble(4, 90); 100 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 101 pstm.addBatch(); 102 // 2) 设置sql语句4 103 pstm.setInt(1, 32); 104 pstm.setString(2,"liug"); 105 pstm.setString(3, "c"); 106 pstm.setDouble(4, 50); 107 // 3) 将一组参数添加到此 PreparedStatement 对象的批处理命令中。 108 pstm.addBatch(); 109 // 4) 将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。 110 pstm.executeBatch(); 111 System.out.println("插入成功!"); 112 // 若成功执行完所有的插入操作,则正常结束 113 con.commit();//2,进行手动提交(commit) 114 System.out.println("提交成功!"); 115 con.setAutoCommit(true);//3,提交完成后回复现场将Auto commit,还原为true, 116 117 } catch (SQLException e) { 118 e.printStackTrace(); 119 try { 120 // 若出现异常,对数据库中所有已完成的操作全部撤销,则回滚到事务开始状态 121 if(!con.isClosed()){ 122 con.rollback();//4,当异常发生执行catch中SQLException时,记得要rollback(回滚); 123 System.out.println("插入失败,回滚!"); 124 con.setAutoCommit(true); 125 } 126 } catch (SQLException e1) { 127 e1.printStackTrace(); 128 } 129 }finally{ 130 JDBCConAndClo.closePreparedStatement(pstm); 131 JDBCConAndClo.closeConnection(con); 132 } 133 } 134} 135
0 0
- [转]Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- Statement和PreparedStatement批量更新
- JDBC批量更新PreparedStatement和Statement
- Statement 和PreparedStatement 批量处理JDBC语句提高处理速度
- statement与preparedStatement 批量操作
- [转]Preparedstatement和Statement的区别
- (转)Statement和PreparedStatement之间的区别
- 【转】深入理解Statement和PreparedStatement
- Statement和PreparedStatement之间的区别 [转]
- 深入 理解 Statement 和 PreparedStatement 【转】
- linphone 编译
- Weka-Fliter类-选择属性变量[4]
- PHPStorm显示代码行号设置
- LINUX常用命令 第一天总结
- Java基础--- 如何配置环境变量
- [转]Statement和PreparedStatement批量更新
- 谷歌page-rank搜索算法 相关知识
- 模板匹配
- LOCAL_LDLIBS报错
- 浏览器端JS导出EXCEL
- <Sicily>Brackets Matching
- WebStorm下载,破解,汉化,debug调试,以及会debug会出现的问题
- Linux中安装PHP的pdo_mysql扩展的详细解决方案
- 1基于getopts和case组合使用