首页 > 数据库技术 > 详细

JDBC的批量添加

时间:2018-05-02 17:19:07      阅读:167      评论:0      收藏:0      [点我收藏+]

数据库

:技术分享图片

技术分享图片

批量添加代码:

package com.hyhl.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.omg.PortableInterceptor.INACTIVE;

import com.hyhl.DrugFactory.vo.DrugMumberDetailVo;
import com.hyhl.patient.util.DataSourceUtils;
import com.hyhl.util.DateTimeUtil;

public class JDBCBatchInsert {
    private String o_url = "jdbc:mysql://localhost:3306/hy_iwrs_test";
    private Connection conn = null;
    private String userName = "root";
    private String password = "root";
    
    /**
     * 使用PreparedStatement做插入
     * @param size
     */
    public void insertByBatchPrepareStat(List<DrugMumberDetailVo> list){
        String startTi = DateTimeUtil.getFormatDateTime(new java.util.Date());
        System.out.println("PreparedStatement做插入的开始时间"+startTi); 
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(o_url, userName, password);
            conn.setAutoCommit(false);
            String sql = "INSERT drug_number_detail_test(random,projectId,groupName,status) VALUES(?,?,?,?)";
            PreparedStatement prest = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            for(DrugMumberDetailVo enter:list){
              prest.setString(1, enter.getRandom());
              prest.setInt(2, enter.getProject());
              prest.setInt(3, enter.getGroup());
              prest.setInt(4, enter.getStatus());
              prest.addBatch();
            }
           prest.executeBatch();
           conn.commit();
           conn.close();
           String startTi1 = DateTimeUtil.getFormatDateTime(new java.util.Date());
           System.out.println("PreparedStatement做插入的结结时间"+startTi1); 
        } catch (SQLException ex) {
            Logger.getLogger(JDBCBatchInsert.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JDBCBatchInsert.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

 测试类:

package com.hyhl.test;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.hyhl.DrugFactory.vo.DrugMumberDetailVo;

public class JDBCBatchInsertTest {
  JDBCBatchInsert jdbcBatchInsert = null;
    public JDBCBatchInsertTest() {
        
    }

    @Before
    public void setUp() {
        jdbcBatchInsert = new JDBCBatchInsert();
    }

    @After
    public void tearDown() {
        jdbcBatchInsert = null;
    }

     @Test
     public void testPrepare() {

         
         List<DrugMumberDetailVo> list=new ArrayList<DrugMumberDetailVo>();
         for(int i=0;i<20000;i++){
             DrugMumberDetailVo query=new DrugMumberDetailVo();
             query.setGroup(0);
             query.setProject(3);
             query.setStatus(1);
              int startNumber=0;
                if(i+1<10){
                    startNumber=Integer.valueOf("2100000"+String.valueOf(i+1));
                }else if (i+1<100){
                    startNumber=Integer.valueOf("210000"+String.valueOf(i+1));
                }else if(i+1<1000){
                    startNumber=Integer.valueOf("21000"+String.valueOf(i+1));
                }else if(i+1<10000){
                    startNumber=Integer.valueOf("2100"+String.valueOf(i+1));
                }else if(i+1<100000){
                    startNumber=Integer.valueOf("210"+String.valueOf(i+1));
                }else if(i+1<1000000){
                    startNumber=Integer.valueOf("21"+String.valueOf(i+1));
                }
                query.setRandom(String.valueOf(startNumber));
             list.add(query);
             
         }
         jdbcBatchInsert.insertByBatchPrepareStat(list);
     }
     

}

效率比for循环插入数据快跟多

 

JDBC的批量添加

原文:https://www.cnblogs.com/wyf-love-dch/p/8981281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!