博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用JDBC的批处理功能
阅读量:4567 次
发布时间:2019-06-08

本文共 2146 字,大约阅读时间需要 7 分钟。

1 package cn.itcast.jdbc; 2  3 import java.sql.Connection; 4 import java.sql.Date; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 10 public class BatchTest {11 12     public static void main(String[] args) throws SQLException {13         /*for (int i = 0; i < 1000; i++) {14             create(i);15         }*/16         createBatch();17     }18 19     static int create(int i) throws SQLException {20         Connection conn = null;21         PreparedStatement ps = null;22         ResultSet rs = null;23 24         try {25             conn = jdbcUtils.getConnection();26 27             String sql = "insert into user(name,birthday,money) values(?,?,?)";28 29             ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);30             ps.setString(1, "name" + i);31             ps.setDate(2, (java.sql.Date) new Date(System.currentTimeMillis()));32             ps.setFloat(3, 1000F + i);33 34             ps.executeUpdate();35 36             rs = ps.getGeneratedKeys();37             int id = 0;38 39             if (rs.next())40                 id = rs.getInt(1);41 42             return id;43 44         } finally {45             jdbcUtils.free(rs, ps, conn);46         }47     }48 49     static void createBatch() throws SQLException {50         Connection conn = null;51         PreparedStatement ps = null;52         ResultSet rs = null;53 54         try {55             conn = jdbcUtils.getConnection();56 57             String sql = "insert into user(name,birthday,money) values(?,?,?)";58 59             ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);60 61             for (int i = 0; i < 1000; i++) {62                 ps.setString(1, "name" + i);63                 ps.setDate(2, new Date(System.currentTimeMillis()));64                 ps.setFloat(3, 1000F + i);65 66                 ps.addBatch();67             }68 69             int[] is = ps.executeBatch();70 71         } finally {72             jdbcUtils.free(rs, ps, conn);73         }74     }75 76 }
BatchTest

 

转载于:https://www.cnblogs.com/aineko/p/3946520.html

你可能感兴趣的文章
Java中的Scoket编程
查看>>
WPF邮件群发工具开发 之 进度条(属性改变通知机制)的实现
查看>>
ubuntu14.04 放开串口权限
查看>>
HttpClient封装工具类
查看>>
机器学习 回归算法
查看>>
SSM博客登录注册
查看>>
在Linux系统上部署发布java web系统(Ubuntu16.04)
查看>>
shell 学习之脚本编写1
查看>>
winForm 程序开发界面参数传递
查看>>
查询计算机启动了多长时间的工具
查看>>
【转】Spring MySQL 事务隔离级别,传播机制,savepoint
查看>>
IOS 开发中的KVC 和KVO
查看>>
05-Python基础之函数基础
查看>>
水晶苍蝇拍:价值投资的“基础,重点和核心” (2010-06-23 18:30:07)
查看>>
HTML超链接的使用
查看>>
h5微信支付在微信内页使用微信公众号支付
查看>>
分区函数Partition By的与row_number()的用法以及与排序rank()的用法详解(获取分组(分区)中前几条记录)(转)...
查看>>
设计模式学习之责任链模式(Chain of Responsibility,行为型模式)(22)
查看>>
AnimatorCompatHelper clearInterpolator
查看>>
Flutter基础Widget之按钮(RaisedButton、FlatButton、OutlineButton,IconButton)
查看>>