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();
}
}
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();//执行sql语句
}
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
}
}
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();//所有语句都执行完毕后才手动提交sql语句
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
}
}
private String url ="jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
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("百万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
}
}
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();//所有语句都执行完毕后才手动提交sql语句
} catch (Exception e) {
e.printStackTrace();
} finally {
erro();
}
System.out.println("百万条数据插入用时:" + (System.currentTimeMillis() - start)+"【单位:毫秒】");
}
}