Java implementation bulk import data mysql database

This article completely copy someone else's.

Original Title: Java implemented batch data into the database (-2 speed optimization method)

Original Address: https://blog.csdn.net/qy20115549/article/details/52699724

 Original looked clearer, please view the original venue.

 

Connect to the database

package db;
import java.sql.Connection;
import java.sql.DriverManager;
/*   
 *  合肥工业大学 管理学院 qianyang [email protected]
 */
public class MySQLConnections {
    private String driver = "";
    private String dbURL = "";
    private String user = "";
    private String password = "";
    private static MySQLConnections connection = null;

    private MySQLConnections() throws Exception {
        driver = "com.mysql.jdbc.Driver";
        dbURL = "jdbc:mysql://127.0.0.1:3306/test";
        user = "root";
        password = "112233";
        System.out.println("dbURL:" + dbURL);
    }

    public static Connection getConnection() {
        Connection conn = null;
        if (connection == null) {
            try {
                connection = new MySQLConnections();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        try {
            Class.forName(connection.driver);
            conn = DriverManager.getConnection(connection.dbURL,
                    connection.user, connection.password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}

 

Two ways batch processing

The first way, every 5000 records in the database, once. That is, each submission has recorded 5000, of course, may not be the last time. In addition, one way is to use the default submission. Both are good processing speed. 50000 records, about 15 seconds.

Package Penalty for db;
 Import java.sql.Connection;
 Import java.sql.PreparedStatement;
 Import java.sql.SQLException;
 / *    
 * Hefei University [email protected] management Qianyang 
 * / 
public  class MySQLUpdate {
     static Connection CON = MySQLConnections. the getConnection ();
     static the PreparedStatement stmt = null ;
     public  static  int executelnsert () throws SQLException {
         int I = 0 ;  
         // set number of batch processing              
        int batchSize = 5000;        
        Stmt = con.prepareStatement ( "INSERT INTO mysqltest (the above mentioned id, name)" 
                + "values (,)??" );
         // close the transaction automatically submitted, this line must be added 
        con.setAutoCommit ( false );
         for ( int 0 = J; J <50005; J ++ ) {
             ++ I; 
            stmt.setInt ( . 1 , J);   
            stmt.setString ( 2, "name" );   
            stmt.addBatch ();   
            IF (I batchSize% == 0 ) { 
                stmt.executeBatch (); 
                con.commit (); 
            } 
        } 
        IF(I = 0% batchSize! ) { 
            Stmt.executeBatch (); 
            con.commit (); 
        } 
        return I; 
    } 

    public  static   void executeInsert2 () throws SQLException {
         // Close the automatic transaction commit, this line must be added   
        con.setAutoCommit ( to false ); 
        stmt = con.prepareStatement ( "INSERT INTO mysqltest1 (ID, name)" 
                + "values (,)??" );
         for ( int J = 0; J <50002; J ++ ) { 
            stmt.setInt ( . 1  , J);  
            stmt.setString ( 2, "name");
            stmt.addBatch(); 
        }
        stmt.executeBatch();
        con.commit();
        stmt.close();   
        con.close();
    }

}

main method

Package Penalty for main;
 Import java.sql.SQLException;
 Import db.MySQLUpdate;
 / *    
 * Hefei University [email protected] management Qianyang 
 * / 
public  class the Test { 

    public  static  void main (String [] args) throws SQLException {
 //       begin1 = System.currentTimeMillis Long (); 
 //       MySQLUpdate.executeInsert ();
 //       Long END1 = System.currentTimeMillis ();  
 //       System.out.println ( "run time is:" + (end1-begin1) ) ; 

        Long begin2 = System.currentTimeMillis (); 
        MySQLUpdate.executeInsert1 ();
        Long end2 = System.currentTimeMillis ();   
        System.out.println ( "run time is:" + (end2- begin2)); 
    } 

}

 

Guess you like

Origin www.cnblogs.com/Tpf386/p/11851002.html