JNBusiness/ruoyi-admin/src/main/java/com/ruoyi/web/utils/JDBCBatch.java

162 lines
6.1 KiB
Java

package com.ruoyi.web.utils;
import com.ruoyi.material.domain.cost;
import com.ruoyi.material.domain.material;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;
@Component
public class JDBCBatch {
private static String databaseURL;
public static String getDatabaseURL() {
return databaseURL;
}
@Value(value = "${spring.datasource.druid.quot.url}")
public void setDatabaseUR(String databaseURL) {
JDBCBatch.databaseURL = databaseURL;
}
private static String username;
public static String getUsername() {return username;}
@Value(value = "${spring.datasource.druid.quot.username}")
public void setUsername(String username) {
JDBCBatch.username = username;
}
private static String password;
public static String getPassword() {
return password;
}
@Value(value = "${spring.datasource.druid.quot.password}")
public void setPassword(String password) {
JDBCBatch.password = password;
}
public static void insertMaterialBatch(List<material> list) throws IOException {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DriverManager.getConnection(databaseURL, username, password);
// 关闭自动提交事务,改为手动提交
connection.setAutoCommit(false);
System.out.println("===== 开始插入数据 =====");
long startTime = System.currentTimeMillis();
String sqlInsert = "insert into c_material(material_id, material_xingh, material_guig, material_diany, material_dw,material_type_id) values (?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sqlInsert);
Random random = new Random();
for (int i = 0; i < list.size(); i++) {
preparedStatement.setInt(1, list.get(i).getMaterial_id());
preparedStatement.setString(2, list.get(i).getMaterial_xingh());
preparedStatement.setString(3, list.get(i).getMaterial_guig());
preparedStatement.setString(4, list.get(i).getMaterial_diany());
preparedStatement.setString(5, list.get(i).getMaterial_dw());
preparedStatement.setString(6, list.get(i).getMaterial_type_id());
// 添加到批处理中
preparedStatement.addBatch();
if (i % 10000 == 0) {
// 每1000条数据提交一次
preparedStatement.executeBatch();
connection.commit();
System.out.println("成功插入第 "+ i+" 条数据");
}
}
// 处理剩余的数据
preparedStatement.executeBatch();
connection.commit();
long spendTime = System.currentTimeMillis()-startTime;
System.out.println("成功插入"+ list.size()+" 条数据,耗时:"+spendTime+"毫秒");
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void insertCostBatch(List<cost> list) throws IOException {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DriverManager.getConnection(databaseURL, username, password);
// 关闭自动提交事务,改为手动提交
connection.setAutoCommit(false);
System.out.println("===== 开始插入数据 =====");
long startTime = System.currentTimeMillis();
String sqlInsert = "insert into c_material_cost(cost_id, cost_material_id, cost_cl_id, cost_cl_qty, cost_cl_qty_2) values (?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sqlInsert);
Random random = new Random();
for (int i = 0; i < list.size(); i++) {
preparedStatement.setInt(1, list.get(i).getCost_id());
preparedStatement.setInt(2, list.get(i).getCost_material_id());
preparedStatement.setString(3, list.get(i).getCost_cl_id());
preparedStatement.setString(4, list.get(i).getCost_cl_qty());
preparedStatement.setString(5, list.get(i).getCost_cl_qty_2());
// 添加到批处理中
preparedStatement.addBatch();
if (i % 10000 == 0) {
// 每1000条数据提交一次
preparedStatement.executeBatch();
connection.commit();
System.out.println("成功插入第 "+ i+" 条数据");
}
}
// 处理剩余的数据
preparedStatement.executeBatch();
connection.commit();
long spendTime = System.currentTimeMillis()-startTime;
System.out.println("成功插入"+ list.size()+" 条数据,耗时:"+spendTime+"毫秒");
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}