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();
        }
    }
    

      

  • 相关阅读:
    Java 必看的 Spring 知识汇总
    java与C#的基础语法区别--持续更新
    MQTT协议-MQTT协议解析(MQTT数据包结构)
    MQTT协议-MQTT协议简介及协议原理
    RabbitMQ与AMQP
    JMS、AMQP和MQTT主要特性
    socket 极值数量
    redis.conf详细说明
    Redis 模糊匹配 SearchKeys
    Parallel
  • 原文地址:https://www.cnblogs.com/leigepython/p/10005689.html
Copyright © 2011-2022 走看看