批处理和获取主键值
来源:互联网 发布:涌金软件 编辑:程序博客网 时间:2024/05/23 23:02
批处理:
1)想发送多条SQL,又要降低与数据库系统的交互,这时使用批处理2)Statement对象:适合对不同结构的SQL做批处理操作
3)PreparedStatement对象:适合对相同结构的SQL做批处理操作
采用PreparedStatement.addBatch()实现批处理
优点:发送的是预编译后的SQL语句,执行效率高。缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数 据,或批量更新表的数据。
案例:
//Statement和PreparedStatment的批处理public class Demo3 {public static void statementBatch() {Connection conn = null;Statement stmt = null;ResultSet rs = null;String insertSQL = "insert into user(username,password,birthday,salary) values('jack','000111','2011-10-26',5000)";String updateSQL = "update user set username='杰克' where username='jack'";try {conn = JdbcUtil.getMySqlConnection();stmt = conn.createStatement();//将需要执行的多条命令加入到批对象中stmt.addBatch(insertSQL);stmt.addBatch(updateSQL);//一次性发送批对象到数据库端执行,返回每条SQL的结果int[] is = stmt.executeBatch();//将批对象清空stmt.clearBatch();//显示结果System.out.println(is[0]+":"+is[1]);} catch (Exception e) {e.printStackTrace();}finally{JdbcUtil.close(rs);JdbcUtil.close(stmt);JdbcUtil.close(conn);}}public static void preparedBatch() {Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;String insertSQL = "insert into user(username,password,birthday,salary) values(?,?,?,?)";try {conn = JdbcUtil.getMySqlConnection();pstmt = conn.prepareStatement(insertSQL);long begin = System.currentTimeMillis();for(int i=1;i<=1000;i++){pstmt.setString(1,"jack"+i);pstmt.setString(2,"111111");pstmt.setDate(3,new java.sql.Date(12345));pstmt.setFloat(4,5000);//加入到批对象中pstmt.addBatch();if(i%100==0){//执行批对象pstmt.executeBatch();//清空批对象pstmt.clearBatch();}}//执行批对象pstmt.executeBatch();//清空批对象pstmt.clearBatch();long end = System.currentTimeMillis();System.out.println((end-begin)/1000+"秒");} catch (Exception e) {e.printStackTrace();}finally{JdbcUtil.close(rs);JdbcUtil.close(pstmt);JdbcUtil.close(conn);}}public static void main(String[] args) {//statementBatch();preparedBatch();}}
获取数据库主键值
1)当需要获取刚插入主键信息的时候,需要使用获取主键值方法2)关键代码:
pstmt = conn.prepareStatement(sqlA,Statement.RETURN_GENERATED_KEYS);
rs = pstmt.getGeneratedKeys();
if(rs.next()){
Long temp = (Long) rs.getObject(1);
pid = temp.intValue();
}
public class Dao {public void save(Person p) throws SQLException {//思路Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;conn = JdbcUtil.getMySqlConnection();String sqlA = "insert into person(name) values(?)";String sqlB = "insert into card(location,pid) values(?,?)";//NO1,向person表插入一条记录pstmt = conn.prepareStatement(sqlA,Statement.RETURN_GENERATED_KEYS);pstmt.setString(1,p.getName());pstmt.executeUpdate();int pid = 0;//NO2,取得插入记录的主键值rs = pstmt.getGeneratedKeys();if(rs.next()){Long temp = (Long) rs.getObject(1);pid = temp.intValue();}//NO3,向card表插入一条记录pstmt = conn.prepareStatement(sqlB);pstmt.setString(1,p.getCard().getLocation());pstmt.setInt(2,pid);pstmt.executeUpdate();//非空关闭流JdbcUtil.close(rs);JdbcUtil.close(pstmt);JdbcUtil.close(conn);}}
1 0
- 批处理和获取主键值
- 批处理,主键,存储过程和事务
- 批处理获取时间值
- GridView获取主键值
- gridview获取主键的值
- gridview获取主键的值
- Spring 主键序列值获取
- Windows批处理中获取日期和时间
- Windows批处理中获取日期和时间
- Windows批处理中获取日期和时间
- rails获取数据库表主键和序列
- JDBC笔记-BLOB和获取主键
- 【JDBC】——【大文本、二进制、批处理、获取自动生成主键、事务、事务的隔离级别】
- 获取mysql数据库中的自增主键属性值和非自增主键属性值的方法
- Mybatis添加功能时获取mysql自增主键和非自增主键的返回值
- 注册表处理之(根据注册表路径获取主键值和子键值)
- 批处理学习--参数值获取方法示例
- 批处理调用exe获取返回值
- File操作(5)---------文件读取,写入 复制(二)
- poj 1260 Pearls
- Spring Boot 1.4.3搭建
- 计时函数clock()
- 详解Android开发中UI的五种布局管理器教程
- 批处理和获取主键值
- 10881 - Piotr's Ants
- Java基本(1)
- LeetCode Group Anagrams
- PLSQL Function
- 欢迎使用CSDN-markdown编辑器
- 2016,青春无悔
- Fusion 360 API:特征与多体模型的布尔操作
- day01_计算机基础知识