jdbc插入数据比较

 

/*
 Navicat Premium Data Transfer

 Source Server         : localhost:3306
 Source Server Type    : MySQL
 Source Server Version : 50624
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50624
 File Encoding         : 65001

 Date: 31/01/2019 10:46:04
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` varchar(222) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;
package com.ce.duo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCUtil {

    private static final String URL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
    private static final String USER="root";
    private static final String PASSWORD="root";
    
    private static Connection conn=null;
    
    static {
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库的连接
            conn=DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //将获得的数据库与java的链接返回(返回的类型为Connection)
    public static Connection getConnection(){
        return conn;
    }
	public static void release( PreparedStatement stmt, Connection conn2) throws SQLException {
		// TODO Auto-generated method stub
		if (stmt!=null) {
			stmt.close();
		}
		if (conn2!=null) {
			conn2.close();
		}
	}
}
package com.ce.duo1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 测试100万条数据 
 *  方法1、普通采用jdbc插入 方法
 *  2、jdbc用事务进行提交  事务提交是把语句一起执行 
 *  方法3、batch语句 内部实现是是把 values 
 */
public class BigDataTest {

	/**
	 * @throws SQLException
	 */
	public void test1() throws SQLException {
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement stmt = null;
		long t1 = System.currentTimeMillis();
		try {
			stmt = conn.prepareStatement("insert into student values(?,?,?)");
			for (int i = 0; i < 100000; i++) {
				stmt.setString(1, i + "张");
				stmt.setString(2, i + "男");
				stmt.setString(3, i + "");
				stmt.execute();
			}
		} finally {
			JDBCUtil.release( stmt, conn);
		}
		long t2 = System.currentTimeMillis();
		System.out.println((t2 - t1));
	}

	public void test2() throws SQLException {
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement stmt = null;
		long t1 = System.currentTimeMillis();
		try {
			conn.setAutoCommit(false);
			stmt = conn.prepareStatement("insert into student values(?,?,?)");
			for (int i = 0; i < 100000; i++) {
				stmt.setString(1, i + "张");
				stmt.setString(2, i + "男");
				stmt.setString(3, i + "");
				stmt.execute();
			}
			conn.commit();
		} finally {
			JDBCUtil.release( stmt, conn);
		}
		long t2 = System.currentTimeMillis();
		System.out.println((t2 - t1));
	}

	/**
	 * @throws SQLException
	 */
	public void test3() throws SQLException {
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement stmt = null;
		long t1 = System.currentTimeMillis();
		try {
			stmt = conn.prepareStatement("insert into student values(?,?,?)");
			for (int i = 0; i < 100000; i++) {
				stmt.setString(1, i + "张");
				stmt.setString(2, i + "男");
				stmt.setString(3, i + "");
				stmt.addBatch();
			}
			stmt.executeBatch();
		} finally {
			JDBCUtil.release( stmt, conn);
		}
		long t2 = System.currentTimeMillis();
		System.out.println((t2 - t1));
	}

	/**
	 * @throws SQLException
	 */
	public void test4() throws SQLException {
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement stmt = null;
		long t1 = System.currentTimeMillis();
		try {
			stmt = conn.prepareStatement("insert into student values(?,?,?)");
			int size = 5000000;
			for (int i = 0; i < size; i++) {
				stmt.setString(1, i + "张");
				stmt.setString(2, i + "男");
				stmt.setString(3, i + "");
				stmt.addBatch();
				if (i % 10000 == 0 || i == size - 1) { 
					stmt.executeBatch();
					stmt.clearBatch();
					/** 清除缓存 */
					System.out.println("执行到第" + i / 10000 + "外循环");
				}
			}
		} finally {
			JDBCUtil.release( stmt, conn);
		}
		long t2 = System.currentTimeMillis();
		System.out.println((t2 - t1));
	}

}
package com.ce.duo1;

import java.sql.SQLException;

public class Test {
	public static void main(String[] args) {
		BigDataTest test=new BigDataTest();
		try {
//			test.test1();//普通方式 时间:10万条
//			test.test2();//二、事务提交 
//			test.test3();//三、批处理: 
			test.test4();// 四、事务+批处理并
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值