`

ImportDataFromMySQLToOracle

阅读更多
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import oracle.jdbc.OraclePreparedStatement;

public class ImportDataFromMySQLToOracle {

	public static void main(String[] args) {

        Connection conn_mysql = null;
		Connection conn_oracle = null;

		String url_mysql = "jdbc:mysql://192.168.91.76:3306/misdb?useUnicode=true&characterEncoding=utf8";
		String user_mysql = "misadmin";
		String password_mysql = "test";

		String url = "jdbc:oracle:thin:@192.168.91.86:1521:WPNA";
		String userName = "test";
		String password = "test";



//		long current = System.currentTimeMillis();

		try {

			Class.forName("com.mysql.jdbc.Driver");
			conn_mysql = DriverManager.getConnection(url_mysql, user_mysql, password_mysql);

			Statement s = conn_mysql.createStatement();
			ResultSet rs = s.executeQuery("SELECT * from TN_SMS_LOG");



			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn_oracle = DriverManager.getConnection(url, userName, password);


			PreparedStatement pstmt = conn_oracle.prepareStatement("INSERT INTO TN_SMS_LOG VALUES(?,?,?,?,?,to_date(substr(?,1,19),'yyyy-mm-dd hh24:mi:ss'),to_date(substr(?,1,19),'yyyy-mm-dd hh24:mi:ss'),?)");

			((OraclePreparedStatement)pstmt).setExecuteBatch(3000);

			int count = 0;

			while (rs.next()) // loop through rows of result set
			{
				int  TN_SMS_LOG_ID =rs.getInt(1);
				String  PTN = rs.getString(2);
				String MESSAGE = rs.getString(3);
				int  CARRIER_CODE = rs.getInt(4);
				int  STATUS =rs.getInt(5);
				String  UPDATE_TIME = rs.getString(6);
				String  CREATE_TIME = rs.getString(7);
				int  SMS_TYPE=rs.getInt(8);

        		pstmt.setInt(1,TN_SMS_LOG_ID);
                pstmt.setString(2,PTN);
                pstmt.setString(3,MESSAGE);
				pstmt.setInt(4,CARRIER_CODE);
				pstmt.setInt(5,STATUS);
				pstmt.setString(6,UPDATE_TIME);
				pstmt.setString(7,CREATE_TIME);
				pstmt.setInt(8,SMS_TYPE);


				pstmt.executeUpdate();

				//pstmt.clearParameters();


				++count;

				if(count%3000==0){
					//System.out.println("Execute batch insert");
					((OraclePreparedStatement)pstmt).sendBatch();
				}

				System.out.println(count);

			}



			//current = System.currentTimeMillis() - current;
			pstmt.close();
			conn_oracle.close();
			rs.close(); // close result set
			s.close(); // close statement

			//System.out.println(count + " mysql rows were returned. " + current/1000 + " seconds has been continued.");

		} catch (Exception e) {
			System.err.println("Cannot connect to server" + e);
		} catch (Error e) {
			e.printStackTrace();
		} finally {
			if (conn_mysql != null) {
				try {
					conn_mysql.close();
					// System.out.println ("Disconnected"); /* for debugging */
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics