package com.data.gz;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import com.data.util.DateUtil;
import com.data.util.JdbcUtil;
public class OracleToSqlite {
static Connection conOraza = null;
static Connection con = null;
static String sqlOraza = "" ;
static String cSqlOraza = "";
static String iSqlOraza = "";
static String uSqlOraza = "";
public static void main(String[] args) {
long total=0;
try {
conOraza = JdbcUtil.getConnectionOraza();
conAsdb = JdbcUtil.getConnectionAsdb();
con = getConnectionSqlite("D:/sqlite/db/","sqlite");
/*test();*/
catcheAllByOraza();
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtil.close(conOraza, null, null);
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
con = null;
}
}
}
public static void catcheAllByOraza() throws Exception{
PreparedStatement psql=null;
ResultSet rsql=null;
//判断是否需要更新
PreparedStatement pcsql=null;
ResultSet rcheck=null;
//更新
PreparedStatement pusql=null;
//插入
PreparedStatement pisql=null;
psql = conOraza.prepareStatement(sqlOraza);
pcsql = con.prepareStatement(cSqlOraza);
pusql = con.prepareStatement(uSqlOraza);
pisql = con.prepareStatement(iSqlOraza);
int num=0;
try {
con.setAutoCommit(flase);
rsql=psql.executeQuery();
while(rsql.next()){
num++;
String id = rsql.getString("id");
java.sql.Date createtm = rsql.getDate("createtm");
java.sql.Date updatetm = rsql.getDate("updatetm");
pcsql.setString(1, id);
rcheck=pcsql.executeQuery();
if(rcheck.next()){
//u
System.out.println("update:"+id);
pusql.setString(1, xm);
pusql.setDate(29, createtm);
pusql.setDate(30, updatetm);
pusql.setString(31, id);
pusql.addBatch();
}else{
//insert
System.out.println("insert:"+id);
pisql.setString(1, id);
pisql.setDate(30, createtm);
pisql.setDate(31, updatetm);
pisql.addBatch();
}
if(num%300==0){
pisql.executeBatch();
pusql.executeBatch();
con.commit();
}
}
pisql.executeBatch();
pusql.executeBatch();
con.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
/*con.commit();*/
con.setAutoCommit(true);
JdbcUtil.close(null, rsql, psql);
JdbcUtil.close(null, rcheck, pcsql);
JdbcUtil.close(null, null, pusql);
JdbcUtil.close(null, null, pisql);
System.out.println("关闭");
}
}
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;
}
public static void test() throws Exception{
con = getConnectionSqlite("D:/sqlite/db/","sqlite");
PreparedStatement ps=null;
ResultSet rs=null;
String sql = "select count(1) num from test";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
System.out.println("num: "+rs.getString("num"));
}
try {
if (rs != null) {
rs.close();
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
rs = null;
con = null;
}
}
}