[转]关于PreparedStatement.addBatch()方法
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用.
1.建立链接,(打电话拨号 )
Connection connection =getConnection();
2.不自动 Commit (瓜子不是一个一个吃,全部剥开放桌子上,然后一口舔了)
// setAutoCommit(true):sql命令的提交(commit)由驱动程序负责
// setAutoCommit(false):sql命令的提交由应用程序负责,程序必须调用commit或者rollback方法
connection.setAutoCommit(false);
3.预编译SQL语句,只编译一回哦,效率高啊.(发明一个剥瓜子的方法,以后不要总想怎么剥瓜子好.就这样剥.)
PreparedStatement statement = connection.prepareStatement("INSERT INTO TABLEX VALUES(?, ?)");
4.来一个剥一个,然后放桌子上
//记录1
statement.setInt(1, 1);
statement.setString(2, "Cujo");
statement.addBatch();
//记录2
statement.setInt(1, 2);
statement.setString(2, "Fred");
statement.addBatch();
//记录3
statement.setInt(1, 3);
statement.setString(2, "Mark");
statement.addBatch();
//批量执行上面3条语句. 一口吞了,很爽
int [] counts = statement.executeBatch();
//Commit it 咽下去,到肚子(DB)里面
connection.commit();
stmt.addBatch("update TABLE1 set 题目="盛夏话足部保健1" where id="3407"");
stmt.addBatch("update TABLE1 set 题目="夏季预防中暑膳食1" where id="3408"");
stmt.addBatch("INSERT INTO TABLE1 VALUES("11","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("12","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("13","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("14","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("15","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("16","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("17","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("18","12","13","","")");
int [] updateCounts=stmt.executeBatch();
cn.commit();
例如:
public static void execteBatch(Connection conn)throws Exception{
String sql1 = "delete from student where id =3 ";
String sql2 = "delete from student where id =5 ";
String sql3 = "delete from student where id =6 ";
String sql4 = "delete from student where id =7 ";
PreparedStatement pstmt = conn.prepareStatement(sql1);
pstmt.addBatch();
pstmt.addBatch(sql2);
pstmt.addBatch(sql3);
pstmt.addBatch(sql4);
pstmt.executeBatch();
};
[转]关于PreparedStatement.addBatch()方法的更多相关文章
- [转载] 关于PreparedStatement.addBatch()方法
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接(打电话拨号) Connecti ...
- 关于PreparedStatement.addBatch()方法 (转)
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...
- 关于PreparedStatement.addBatch()方法
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...
- JAVA的PreparedStatement和addBatch()方法
本文介绍两个内容,为什么使用PreparedStatement的addBatch()方法?以及使用PreparedStatement的好处. 一.addBatch使用方法 昨天用JAVA做了一个导表的 ...
- 使用JDBC的addBatch()方法提高效率
在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void addBatch(String sql)将给定的 SQ ...
- Java中数据库连接的一些方法资料汇总
Java中Connection方法笔记 http://www.cnblogs.com/bincoding/p/6554954.html ResultSet详解(转) https://www.cnbl ...
- 理解 Statement 和 PreparedStatement
java,servlet中的PreparedStatement 接口继承了Statement,并与之在两方面有所不同:有人主张,在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以Prepar ...
- preparedStatement和Statement 有什么不一样
1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象. 2.作 ...
- Statement和PreparedStatement批量更新
优势:1.节省传递时间. 2.并发处理. PreparedStatement: 1) addBatch()将一组参数添加到PreparedStatement对象内部. 2) executeBatch( ...
随机推荐
- BZOJ1369:[Baltic2003]Gem(树形DP)
Description 给出一棵树,要求你为树上的结点标上权值,权值可以是任意的正整数 唯一的限制条件是相临的两个结点不能标上相同的权值,要求一种方案,使得整棵树的总价值最小. Input 先给出一个 ...
- 【[CQOI2018]解锁屏幕】
状压这个东西好像没有什么能优化的高级东西,像什么斜率优化,单调队列在状压的优化上都很少见 而最常见的状压优化就是预处理优化了, 这道题就预处理一下所有点对之间连线上的点,之后压成状态就能做到\(O(2 ...
- virtualbox+vagrant学习-4-Vagrantfile-7-WinRM Settings
WinRM Settings 配置命名空间config.winrm config.winrm配置与如何通过winrm配置vagrant访问你的Windows客户相关.与大多数vagrant设置一样,默 ...
- IE6/IE7不支持first-child的解决办法
#sidebar li:first-child{ border-top-style:none; } #sidebar li{ border-top-width:1px; border-top-styl ...
- VC++获取一个GB级大文件的字节大小
常规的获得小文件(2.1GB以下)的字节大小可以使用ftell,函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数.使用fseek函数后再调用函数ftell()就能非常容易地确定文 ...
- HDU 1232 畅通工程(道路连接)(裸并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- EF Core中外键关系的DeleteBehavior介绍(转自MSDN)
Delete behaviors Delete behaviors are defined in the DeleteBehavior enumerator type and can be passe ...
- 如何快速找到指定端口被哪个程序占用并释放该端口(解决bindException)
首先打开打开任务管理器,选择性能模块,下方有打开资源监视器,或者直接搜索资源监视器 在资源监视器中点击侦听端口模块,即可看到正在使用网络端口的应用程序名和pid,如果被占用可以直接使用命令行关闭即可 ...
- 2019年,iOS开发的你不可或缺的进阶之路!
序言 我相信很多人都在说,iOS行业不好了,iOS现在行情越来越难了,失业的人比找工作的人还要多.失业即相当于转行,跳槽即相当于降低自己的身价.那么做iOS开发的你,你是否在时刻准备着跳槽或者转行了. ...
- 工具 | Axure基础操作 No.6
这个是基础教程最后一篇,但是这仅仅是个开始,需要学的东西还有很多.坚持! 1.生成部分原型页面 不能单独生成子级的页面,会自动的勾选上父级.如果想单独的生成的话,就得把这个页面的级别提高,变成一级页面 ...