关于PreparedStatement.addBatch()方法 (转)
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用.
1.建立链接,(打电话拨号 )
Connection connection =getConnection();
2.不自动 Commit (瓜子不是一个一个吃,全部剥开放桌子上,然后一口舔了)
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();
};
本文转自:http://blog.csdn.net/blueling51/article/details/6928755。
关于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( ...
随机推荐
- pip安装的python扩展模块自定义目录
根据系统不同: Windows是python目录下Lib\site-packages\: Linux是/usr/local/lib/python/dist-packages/.
- npm常用命令汇总
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准. 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install ...
- [Spring] Java spring quartz 定时任务
首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath ...
- (转) 打开声音设备需要使用waveOutOpen函数
转自:http://blog.csdn.net/nokianasty/article/details/8558151 打开声音设备 打开声音设备需要使用waveOutOpen函数(可以在您的文档中查到 ...
- bootstrap -- meta中的viewport指令
在查看bootstrap教程中,碰到 <meta name="viewport" content="width=device-width, initial-scal ...
- hdu 4463 Outlets
#include<bits/stdc++.h> using namespace std; double x[100+5],y[100+5]; double e[100+5][100+5]; ...
- 【Java面试题】24 sleep() 和 wait() 有什么区别? 详细解析!!!!
第一种解释: 功能差不多,都用来进行线程控制,他们最大本质的区别是:sleep()不释放同步锁,wait()释放同步缩. 还有用法的上的不同是:sleep(milliseconds)可 ...
- iOS 图文混排 (Swift版)
// 0> 图片附件 let attachment = NSTextAttachment() attachment.image = #imageLiteral(resourceName: &qu ...
- Android ContentProvider、ContentResolver和ContentObserver的使用
1.ContentProvider.ContentResolver和ContentObserver ContentProvider是Android的四大组件之中的一个,可见它在Android中的作用非 ...
- 总结ASP.NET C#中经常用到的13个JS脚本代码
1.按钮前后台事件 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click ...