最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)

 更新時間:2022年11月17日 11:37:03   作者:子非魚呀  
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù),文中的示例代碼講解詳細,對我們學習或工作有一定借鑒價值,需要的可以參考一下

mysql數(shù)據(jù)庫準備

private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    private String user = "root";
    private String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    //封裝與數(shù)據(jù)庫建立連接的類
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    //封裝異常類
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方式一:普通插入

package com.wt;
 
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
/**
 * @Author wt
 * @Date 2022/11/14 21:17
 * @PackageName:com.wt
 * @ClassName: TestAddBatch01
 * @Description: TODO
 * @Version 1.0
 */
public class TestAddBatch01 {
    private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai";
    private String user = "root";
    private String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Test
    public void ccc(){
        long start = System.currentTimeMillis();
        String sql = "insert into a(id, name) VALUES (?,null)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            for (int i = 1; i <= 1000000; i++) {
                ps.setObject(1, i);//填充sql語句種得占位符
                ps.execute();//執(zhí)行sql語句
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            erro();
        }
        System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
 
    }
 
}

用時:62分鐘多 

方式二:使用批處理插入

package com.wt;
 
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
/**
 * @Author wt
 * @Date 2022/11/14 20:25
 * @PackageName:com.wt.util
 * @ClassName: TestAddBatch
 * @Description: TODO
 * @Version 1.0
 */
public class TestAddBatch {
    private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai";
    private String user = "root";
    private String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Test
    public void ccc(){
        long start = System.currentTimeMillis();
        String sql = "insert into a(id, name) VALUES (?,null)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
//            connection.setAutoCommit(false);//取消自動提交
            for (int i = 1; i <= 1000000; i++) {
                ps.setObject(1, i);
                ps.addBatch();
 
                if (i % 1000 == 0) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }
            ps.executeBatch();
            ps.clearBatch();
//            connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            erro();
        }
        System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
 
    }
 
 
}

方式一、二總結:到此可以看出其實其處理程序及批處理是沒有起作用的,為此我們使用方式三

方式三:通過連接配置url設置【&rewriteBatchedStatements=true】(設置重寫批處理語句)

url地址后注意添加【&rewriteBatchedStatements=true】

private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";

方法三較于方法二的改變是只是url地址上的改變,其它沒有任何修改 

package com.wt;
 
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
/**
 * @Author wt
 * @Date 2022/11/14 20:25
 * @PackageName:com.wt.util
 * @ClassName: TestAddBatch
 * @Description: TODO
 * @Version 1.0
 */
public class TestAddBatch {
    private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    private String user = "root";
    private String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Test
    public void ccc(){
        long start = System.currentTimeMillis();
        String sql = "insert into a(id, name) VALUES (?,null)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            for (int i = 1; i <= 1000000; i++) {
                ps.setObject(1, i);
                ps.addBatch();
 
                if (i % 1000 == 0) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }
            ps.executeBatch();
            ps.clearBatch();
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            erro();
        }
        System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
 
    }
 
 
}

用時:【10秒左右】

到此批處理語句才正是生效

注意

數(shù)據(jù)庫連接的url設置了【&rewriteBatchedStatements=true】時,java代碼種的sql語句不能有分號【;】號,否則批處理語句打包就會出現(xiàn)錯誤,導致后面的sql語句提交出現(xiàn)【BatchUpdateException】異常

方式四:通過數(shù)據(jù)庫連接取消自動提交,手動提交數(shù)據(jù)

package com.wt;
 
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
/**
 * @Author wt
 * @Date 2022/11/14 20:25
 * @PackageName:com.wt.util
 * @ClassName: TestAddBatch
 * @Description: TODO
 * @Version 1.0
 */
public class TestAddBatch {
    private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    private String user = "root";
    private String password = "root";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = DriverManager.getConnection(url,user,password);
    }
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @Test
    public void ccc(){
        long start = System.currentTimeMillis();
        String sql = "insert into a(id, name) VALUES (?,null)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            connection.setAutoCommit(false);//取消自動提交
            for (int i = 1; i <= 1000000; i++) {
                ps.setObject(1, i);
                ps.addBatch();
 
                if (i % 1000 == 0) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }
            ps.executeBatch();
            ps.clearBatch();
            connection.commit();//所有語句都執(zhí)行完畢后才手動提交sql語句
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            erro();
        }
        System.out.println("百萬條數(shù)據(jù)插入用時:" + (System.currentTimeMillis() - start)+"【單位:毫秒】");
 
    }
 
 
}

 用時:【9秒左右】

總結:

1.使用批量提交數(shù)據(jù),url一定要設置允許重寫批量提交【rewriteBatchedStatements=true】,以及此時的sql語句一定不能有分號,否則有【BatchUpdateException】異常,

2.其他的就正常使用PreparedStatement ps;的以下三個方法即可

  • ps.addBatch();      將sql語句打包到一個容器中
  • ps.executeBatch();  將容器中的sql語句提交
  • ps.clearBatch();    清空容器,為下一次打包做準備

以上就是Java實現(xiàn)十秒向MySQL插入百萬條數(shù)據(jù)的詳細內容,更多關于Java MySQL插入數(shù)據(jù)的資料請關注腳本之家其它相關文章!

相關文章

最新評論

延安市| 黑龙江省| 南乐县| 万州区| 延津县| 新昌县| 盐边县| 万宁市| 东山县| 长武县| 蒙城县| 资阳市| 天峻县| 古田县| 延边| 岳西县| 扎鲁特旗| 镇安县| 临泽县| 思南县| 千阳县| 华池县| 广宗县| 葵青区| 东乡族自治县| 大石桥市| 东台市| 林西县| 中阳县| 定结县| 德格县| 修水县| 安图县| 镇宁| 色达县| 阳信县| 洞头县| 鲁甸县| 台北县| 砀山县| 云梦县|