1. 程式人生 > >關於JDBC+MySQL批量寫入: 用insert values方式批量寫入

關於JDBC+MySQL批量寫入: 用insert values方式批量寫入

恩,萌新剛來,聽學長說寫部落格可以總結梳理自己的知識,所以來試試,自娛自樂,不喜莫噴。目前還是大二狗,學Java半年多,錯誤很多,望大神指正。微笑

應為經常要寫入大量資料所以做了一個批量寫入測試,這篇文章是為另一篇一個6分鐘爬去8萬條資料的多執行緒Java爬蟲中的批量寫入做說明(還未更新上來請稍後):

我用insert values這種,做過效能對比,程式碼和結果如下:

import java.sql.*;

/**
 * Created by Me on 2017/4/7.
 */
public class sqltest {
    private Connection connection=null;
    public static void main(String[] args) throws SQLException {
        sqltest sqltest = new sqltest();
        sqltest.connectionToSql();
//        sqltest.CrestDatabase("tao");

//        sqltest.creatTable("test");
        System.out.println("1:++++++++++++");
        sqltest.creatTable("tao01");
        for(int i=0;i<6;i++) {
            long start = System.currentTimeMillis();
            sqltest.insert(10000);
            long end = System.currentTimeMillis();
            System.out.print(start - end);
            System.out.println();
        }
        System.out.println("2:++++++++++++");
        sqltest.creatTable("tao02");
        for(int i=0;i<2;i++) {
            long start1 = System.currentTimeMillis();
            sqltest.insert1(10000);
            long end1 = System.currentTimeMillis();
            System.out.print(start1 - end1);
            System.out.println();
        }
        System.out.println(sqltest.count());
    }
    public boolean connectionToSql(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tao?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","123456");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return  false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public boolean creatTable(String name){
        StringBuilder s = new StringBuilder();
        s.append("CREATE TABLE ");
        s.append(name);
        s.append(" ( id int not null, name varchar(64) , myText varchar(64))");
        try {
            Statement statement = connection.createStatement();
            statement.execute(String.valueOf(s));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }
    public boolean CrestDatabase(String database){
        try {
            Statement statement = connection.createStatement();
            String sql = "CREATE DATABASE "+database;
            statement.executeUpdate(sql);
//            String sql1 = "USING "+database;
//            statement.executeUpdate(sql1);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    public void insert(int num){
        StringBuilder s = new StringBuilder();
        s.append("insert into tao01 values ");
        for(int i=0;i<num;i++){
            s.append("(");
            s.append(i);
            s.append(",'test','test'),");
        }
        s.append("(");
        s.append(10000);
        s.append(",'test','test')");
        try {
            Statement statement = connection.createStatement();
            statement.execute(String.valueOf(s));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void insert1(int num){
        StringBuilder s =null;
        for(int i=0;i<num;i++) {
            s = new StringBuilder();
            s.append("insert into tao02 values ");
            s.append("(");
            s.append(i);
            s.append(" ,'test','test')");
            try {
                Statement statement = connection.createStatement();
                statement.execute(String.valueOf(s));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public int count(){
        try{
            Statement statement = connection.createStatement();
            String sql = "SELECT COUNT(*) FROM TAO01";
            ResultSet resultSet = statement.executeQuery(sql);
            resultSet.next();
            return resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}

結果如下:(單位:毫秒)

1:++++++++++++
-618
-815
-161
-216
-230
-200
2:++++++++++++
-445177

可以看出同樣寫入10000條資料,批量寫入時普通寫入的100~200倍,本來打算普通寫入也做三次的,但是太慢了我忍不住關了