public void sync_table_test_thread() throws SQLException, InterruptedException {
long start = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取要迁移oracle表数据库配置
Connection connection = DruidJdbcUtils.getConnection("db0");
Integer count = 0;
PreparedStatement countStatement = connection.prepareStatement("select count (*) from Table \n");
ResultSet countSet = countStatement.executeQuery(); //查询要迁移数据count总数
while (countSet.next()) {
count = countSet.getInt(1);//获取迁移数据count总数
boolean next = true;
try {
if (count>0) {
try {
//设置20个线程 但是分开之后向下取整 那么可能会多一个
int threadCount = 20;//默认设置20个线程
int listSize = (int) Math.floor((double) count / threadCount) == 0 ? 1 : (int) Math.floor((double) count / threadCount);//设置每个线程执行多少条数据,总数据除以线程数=每个线程执行数
PreparedStatement preparedStatement = connection.prepareStatement("select * from TABLE \n");//查询并获取要迁移的总数据
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData md = resultSet.getMetaData();//ResultSetMetaData获取列表字段数、字段参数
int colnum = md.getColumnCount();//获取列表字段数:如1行有10个参数值
List listOfRows = new ArrayList();
while (resultSet.next()) {
&n
java多线程把数据迁移到不同数据库中
于 2024-07-25 16:24:22 首次发布