package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.data.util.JdbcUtil;
public class OracleToSqlite extends Thread{
private String startRow = "";
private String endRow = "";
public OracleToSqlite(String startRow, String endRow) {
super();
this.startRow = startRow;
this.endRow = endRow;
}
/**
* 連接sqlite數據庫
* @param absolutePath
* @param sqliteName
* @return
*/
public static Connection getConnectionSqlite(String absolutePath, String sqliteName){
Connection co=null;
try {
String url ="jdbc:sqlite:" + absolutePath + sqliteName +".db";
Class.forName("org.sqlite.JDBC");
co = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return co;
}
/**
* 創建sqlite的數據庫表
*/
public static void init(){
String sql = "create table Test( idcard VARCHAR2(50), name VARCHAR2(50));";
try (Connection toCon = getConnectionSqlite("E:/sqlite/db/","yh");
PreparedStatement ps=toCon.prepareStatement(sql);){
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void run() {
String fromSql = " select total.idcard, total.name" +
" from" +
" ( select row_limit.*, rownum rownum_ " +
" from " +
" ( select t.idcard," +
" t.name" +
" from Test t " +
" order by t.idcard, t.name " +
" ) row_limit" +
" where rownum <?" +
" )total" +
" where rownum_ >=?" ;
String toSql = "insert into Test(" +
" idcard, name" +
" ) values (" +
" ?, ?)";
ResultSet fromResultSet = null;
try(Connection fromCon = JdbcUtil.getConnectionGzzh();
Connection toCon = getConnectionSqlite("E:/sqlite/db/","yh");
PreparedStatement fromPreparedStatement = fromCon.prepareStatement(fromSql);
PreparedStatement toPreparedStatement = toCon.prepareStatement(toSql)){
toCon.setAutoCommit(false);
fromPreparedStatement.setString(1, endRow);
fromPreparedStatement.setString(2, startRow);
fromResultSet = fromPreparedStatement.executeQuery();
int num = 0;
while(fromResultSet.next()){
num++;
String idcard = fromResultSet.getString("idcard");
String name = fromResultSet.getString("name");
toPreparedStatement.setString(1, idcard);
toPreparedStatement.setString(2, name);
toPreparedStatement.addBatch();
if(num % 3000 == 0){
System.out.println(num);
toPreparedStatement.executeBatch();
toCon.commit();
}
}
System.out.println("end:"+num);
toPreparedStatement.executeBatch();
toCon.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(null, fromResultSet, null);
}
};
public static void main(String[] args) {
try {
init();
Thread.sleep(3000);
new OracleToSqlite("0", "100000").start();
new OracleToSqlite("100000", "200000").start();
new OracleToSqlite("200000", "300000").start();
new OracleToSqlite("300000", "400000").start();
new OracleToSqlite("400000", "500000").start();
new OracleToSqlite("500000", "600000").start();
new OracleToSqlite("600000", "700000").start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}