首页 > 数据库技术 > 详细

oracle数据迁移到mysql

时间:2019-01-07 23:58:32      阅读:308      评论:0      收藏:0      [点我收藏+]

  今天遇到需求要把oracle的部分数据搬到mysql,用java代码抓数据,然后拼接成sql语句,然后用navicat执行sql脚本的方法,导入数据库。

import oracle.jdbc.driver.OracleDriver;

import java.sql.*;
import java.util.Properties;

public class DataTransfer {
    public static void main(String[] args) {
        Connection connect = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Driver driver = new OracleDriver();
            DriverManager.deregisterDriver(driver);
            Properties pro = new Properties();
            pro.put("user", "*******");
            pro.put("password", "*******");
            connect = driver.connect("jdbc:oracle:thin:@ip:port:orcl", pro);
            statement = connect.createStatement();
            resultSet = statement.executeQuery("select user_login_name, user_password, user_real_name, user_status, create_time from CFG_USER");
            while (resultSet.next()) {
                String s = "insert into system_user(user_account, user_password, user_name, user_status, user_create_datetime, user_register_ip) values(";
                String log = resultSet.getString("user_login_name");
                s = s + "‘" + log + "‘" + ",";
                String password = resultSet.getString("user_password");
                s = s + "‘" + password + "‘" + ",";
                String realName = resultSet.getString("user_real_name");
                s = s + "‘" + realName + "‘" + ",";
                String userStatus = resultSet.getString("user_status");
                s = s + userStatus + ",";
                Date create_time = resultSet.getDate("create_time");
                if (create_time == null) {
                    s = s + "null" + ",";
                } else {
                    s = s + "‘" + create_time.toString() + "‘" + ",";
                }
                s = s  +"‘"+ "127.0.0.1" + "‘" + ")";
                System.out.println(s + ";");  //打印输出结果集

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connect != null) connect.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

以上基于我的数据库字段,可根据需求选用恰当的字段。

maven依赖:

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.5.0</version>
        </dependency>    

依赖最好找跟那个数据库版本相同的

技术分享图片

然后navicat执行下拼接好的脚本即可。

 

oracle数据迁移到mysql

原文:https://www.cnblogs.com/chenmz1995/p/10236562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!