oracle:sqlite:表数据迁移(笨办法实现:java)

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;
		}
		
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值