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();

    };

关于PreparedStatement.addBatch()方法的更多相关文章

  1. [转载] 关于PreparedStatement.addBatch()方法

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接(打电话拨号) Connecti ...

  2. 关于PreparedStatement.addBatch()方法 (转)

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...

  3. [转]关于PreparedStatement.addBatch()方法

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...

  4. JAVA的PreparedStatement和addBatch()方法

    本文介绍两个内容,为什么使用PreparedStatement的addBatch()方法?以及使用PreparedStatement的好处. 一.addBatch使用方法 昨天用JAVA做了一个导表的 ...

  5. 使用JDBC的addBatch()方法提高效率

    在批量更新SQL操作的时候建议使用addBatch,这样效率是高些,数据量越大越能体现出来 Statement接口里有两个方法:void     addBatch(String sql)将给定的 SQ ...

  6. Java中数据库连接的一些方法资料汇总

    Java中Connection方法笔记 http://www.cnblogs.com/bincoding/p/6554954.html ResultSet详解(转)  https://www.cnbl ...

  7. 理解 Statement 和 PreparedStatement

    java,servlet中的PreparedStatement 接口继承了Statement,并与之在两方面有所不同:有人主张,在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以Prepar ...

  8. preparedStatement和Statement 有什么不一样

    1. PreparedStatement接口继承Statement, PreparedStatement 实例包含已编译的 SQL 语句,所以其执行速度要快于 Statement 对象.    2.作 ...

  9. Statement和PreparedStatement批量更新

    优势:1.节省传递时间. 2.并发处理. PreparedStatement: 1) addBatch()将一组参数添加到PreparedStatement对象内部. 2) executeBatch( ...

随机推荐

  1. Python 官方团队在打包项目中踩过的坑

    花下猫语:这是 packaging 系列的第三篇译文,该系列是全网关于此话题的最详尽(水平也很高)的一个系列.原作者是 Python 官方打包团队成员,是 virtualenv 和 tox 项目的维护 ...

  2. JDK1.8中的HashMap实现

    1.HashMap概述 在JDK1.8之前,HashMap采用数组+链表实现,即使用链表处理冲突,同一hash值的节点都存储在一个链表里.但是当位于一个桶中的元素较多,即hash值相等的元素较多时,通 ...

  3. 「CH2501」 矩阵距离 解题报告

    CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...

  4. Codeforces Round #519 by Botan Investments(前五题题解)

    开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次to ...

  5. MySQL数据库(三)

    前提要述:参考书籍<MySQL必知必会> 2.1 MySQL简介 2.1.1 什么是MySQL MySQL是一种关系数据库管理系统.负责数据库中数据的存储,检索,管理和处理. 2.1.2 ...

  6. C++string中find,find_first_of和find_last_of的用法

    1. size_t find (const string& str, size_t pos = 0) str.find(str1) 说明:从pos(默认是是0,即从头开始查找)开始查找,找到第 ...

  7. Jquery实现图片管理

    这里实现的是一个图片的在线管理,类似于网络相册的图片管理. 效果图如下: 文件结构如下图: style2.css文件内容如下: @charset "utf-8"; *{;; } i ...

  8. 哪些工具可以提升PHP开发效率

    本文就我自己在开发过程中的一点经验,谈谈如何利用工具来提升开发工作的编码效率, IDE(phpstorm 收费) 一个好的IDE真的可以给开发人员节省大量的时间,我从最开始使用editplus 到su ...

  9. 通过例子进阶学习C++(六)你真的能写出约瑟夫环么

    本文是通过例子学习C++的第六篇,通过这个例子可以快速入门c++相关的语法. 1.问题描述 n 个人围坐在一个圆桌周围,现在从第 s 个人开始报数,数到第 m 个人,让他出局:然后从出局的下一个人重新 ...

  10. 初探ASP.NET Core 3.x (1) - 关于ASP.NET Core

    I 什么是ASP.NET Core ASP.NET is an open source web framework, created by Microsoft, for building modern ...