MySQL測試數據批量添加

應用場景:
測試索引對查找速度的影響

package com.dlj.test;

import java.sql.PreparedStatement;
import java.util.UUID;

import com.dlj.utils.DBUtil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class UuidTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		//根據目標表中所包含的字段,準備sql
		String sql = "insert into t_uuid(uuid, create_time, update_time) values (?, now(), now())";
		try {
			connection = (Connection) DBUtil.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			//利用for循環控制添加記錄的條數
			for (int i = 0; i < 1000000; i++) {
				String uuid = UUID.randomUUID().toString();//獲取UUID
				System.out.println(uuid);
				preparedStatement.setString(1, uuid);
				//執行sql
				boolean success = preparedStatement.execute();
				//打印執行結果
				System.out.println("執行結果:" + success);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章