jdbc批量导入数据

/**
  * 设置数据库连接工具类
  * 包括(连接、注册驱动、释放连接)
  * 
  * 
  */
public  final  class  DBUtils {
     private  static  String mysqlurl =  "jdbc:mysql://....../mytest" ;
     private  static  String accessurl =  "jdbc:mysql://...../mytest" ;
     private  static  String user =  "root" ;
     private  static  String password =  "root" ;
  
     // 获得连接
     public  static  Connection getAccessConn()  throws  SQLException {
         return  DriverManager.getConnection(accessurl, user, password);
     }
      
     public  static  Connection getMySqlConn()  throws  SQLException {
         return  DriverManager.getConnection(mysqlurl, user, password);
     }
  
     // 释放连接
     public  static  void  free(ResultSet rs, PreparedStatement ps, Connection conn) {
         try  {
             if  (rs !=  null ) {
                 rs.close();
             }
         catch  (SQLException e) {
             e.printStackTrace();
         finally  {
             try  {
                 if  (ps !=  null ) {
                     ps.close();
                 }
             catch  (SQLException e) {
                 e.printStackTrace();
             finally  {
  
                 try  {
                     if  (conn !=  null ) {
                         conn.close();
                     }
                 catch  (SQLException e) {
                     e.printStackTrace();
                 }
  
             }
  
         }
  
     }
  
     // 加载驱动
     static  {
         try  {
             Class.forName( "com.mysql.jdbc.Driver" );
         catch  (ClassNotFoundException e) {
             System.out.println( "驱动加载出错" );
         }
     }
}


 

使用测试类

public  class  BatchExample {
     private  static  Connection mysqlConn =  null ;
     private  static  ResultSet rs =  null ;
     // 总条数
     private  static  int  allCount =  10000 ;
     // 分批条数
     private  static  int  preCount =  1000 ;
     // 计数器
     private  static  int  count =  0 ;
  
     private  static  String insertSQL =  "insert into users(firstname, lastname, age) values(?, ?, ?)" ;
  
     private  static  PreparedStatement mysqlPs =  null ;
  
     public  static  void  main(String[] args)  throws  SQLException {
         try  {
             mysqlConn = DBUtils.getMySqlConn();
             mysqlPs = mysqlConn.prepareStatement(insertSQL);
                        //执行操作不提交,最后通过手动进行提交事务
             mysqlConn.setAutoCommit( false );
             long  start = System.currentTimeMillis();
              
             for  ( int  i =  1 ; i <= allCount; i++) {
                 mysqlPs.setString( 1 "firstname"  + i);
                 mysqlPs.setString( 2 "lastname"  + i);
                 mysqlPs.setInt( 3 23 );
                               //先将数据暂存起来
                 mysqlPs.addBatch();        
                 if  ((i % preCount) ==  0 ){
                                      //将暂存的数据进行批量执行
                     mysqlPs.executeBatch();
                     System.out.println( "当前进行完毕===>"  + (++count) * preCount +  "条" );
                 }
             }        
             long  end = System.currentTimeMillis();
             System.out.println( "数据导入完毕,所用时间为: "  + (end - start) +  " ms" );
  
         catch  (Exception e) {
             mysqlConn.rollback();
             System.out.println( "数据出错,已进行回滚" );
             throw  new  RuntimeException();
         finally  {
                        //进行事务提交
             mysqlConn.commit();
             DBUtils.free(rs, mysqlPs, mysqlConn);
         }
     }
  
}


 

猜你喜欢

转载自liuzhiqiang19890403.iteye.com/blog/2072049