/*
*
*使用PreparedStatement实现批量数据的操作
*update、delete本身就具有批量操作的效果
*此时的批量操作,主要指的是批量插入。使用PreparedStatement如何实现更高效的批量插入?
*
*
* 题目:向goods表中插入20000条数据
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(25)
);
* 方式一:使用Statement
* Connection conn = JDBCUtils.getConnection();
* Statement st = conn.createStatement();
* for(int i =1;i<=20000;i++){
* String sql = "insert into goods(name) values('name_"+i+"')";
* st.execute(sql);
* }
*@author 刘鑫
*@create by 2021/5/8 16:10
*/
public class InsertTest {
//批量插入的方式二:使用PreparedStatement
@Test
public void testInsert1(){
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
String sql = "insert into goods(name) values(?)";
ps = conn.prepareStatement(sql);
for (int i=1;i<=20000;i++){
ps.setObject(1,"name_"+i);
ps
jdbc 使用PreparedStatement实现批量数据的操作
最新推荐文章于 2023-11-03 17:50:04 发布