zoukankan      html  css  js  c++  java
  • JDBC批量插入

    package com.jdbc.test;
    
    import java.sql.*;
    
    public class Demo02 {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Connection connection = null;
    
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            Demo02 demo02 = new Demo02();
            demo02.connect();
        }
    
        public void connect() throws ClassNotFoundException, SQLException {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdjk?useSSL=false", "root", "kkk");
            /**
             * 批量插入
             */
    
            long start = System.currentTimeMillis();
            connection.setAutoCommit(false);
    
            preparedStatement = connection.prepareStatement("insert into jdjk_java(name,age,email) values (?,?,?)");
            for (int i = 0; i <= 2000000; i++) {
                preparedStatement.setString(1, "javademo");
                preparedStatement.setInt(2, i);
                preparedStatement.setString(3, "wangzhilei@jd.com");
                preparedStatement.addBatch();
            }
    
            preparedStatement.executeBatch();
    
            connection.commit();
            long end = System.currentTimeMillis();
    
            System.out.println("插入时长" + (end - start));
    
            preparedStatement.close();
    
            // 关闭连接
            connection.close();
        }
    }
    

      

  • 相关阅读:
    3.java开发环境配置
    2.java主要特性
    1.java中main函数理解
    测试项目团队角色岗位职责
    单身程序员
    软件测评师考试
    vue父子组件通信
    python偏函数使用
    Numpy+Pandas读取数据
    chrome无界面模式headless配置
  • 原文地址:https://www.cnblogs.com/leigepython/p/10005689.html
Copyright © 2011-2022 走看看