使用 JDBC 将 table 从 SQL 服务器复制到 Oracle

Copying table from SQL Server into Oracle using JDBC

我正在尝试将三个单独的表从 SQL Server 2012 加载到 Oracle 中。我已经在单独的 类 中建立了与 SQL 服务器和 Oracle 的连接,如下所示:

SQL 服务器连接:

public class TestSqlUtil {
    public Connection getConnection() throws SQLException, IOException
    {
        String propsFile = "tasmania.properties";
        Properties props = new Properties();
        InputStream inputStream = 
            this.getClass().getClassLoader().getResourceAsStream(propsFile);

        if (inputStream == null)
        {
            throw new FileNotFoundException("property file '" + propsFile
                + "' not found in the classpath");
        }

        props.load(inputStream);

        String dblogin = props.getProperty("sqlfdb.LOGIN");
        String dbpasswd = props.getProperty("sqlfdb.PASSWD");
        String jdbcDrv = props.getProperty("sqlfdb.JDBCOCIDRV");
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        Connection con = DriverManager.getConnection(jdbcDrv, dblogin, dbpasswd);
        System.out.printf("successfull connection");
        System.out.println();
        return con;
    }
    public void cleanUp(Connection con, PreparedStatement ps, ResultSet rs)
        throws SQLException
    {
        if (rs != null) rs.close();
        if (ps != null) ps.close();
        if (con != null) con.close();
    }
    public static void main(String a[]) throws SQLException, IOException
    {
        DaoUtil tasmaniaUtil = new DaoUtil();
        tasmaniaUtil.getConnection();
    }
}

Oracle 连接:

public class DaoUtil
{
    public Connection getConnection() throws SQLException, IOException 
    {
        String propsFile = "tasmania.properties";
        Properties props = new Properties();
        InputStream inputStream = 
            this.getClass().getClassLoader().getResourceAsStream(propsFile);

        if (inputStream == null)
        {
            throw new FileNotFoundException("property file '" + propsFile
                + "' not found in the classpath");
        }

        props.load(inputStream);

        String dblogin = props.getProperty("intfdb.LOGIN");
        String dbpasswd = props.getProperty("intfdb.PASSWD");
        String jdbcDrv = props.getProperty("intfdb.JDBCOCIDRV");
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        Connection con = DriverManager.getConnection(jdbcDrv, dblogin, dbpasswd);
        System.out.printf("successfull connection");
        System.out.println();
        return con;
    }
    public void cleanUp(Connection con, PreparedStatement ps, ResultSet rs)
        throws SQLException 
    {
        if (rs != null) rs.close();
        if (ps != null) ps.close();
        if (con != null) con.close();
    }

    public static void main(String a[]) throws SQLException, IOException
    {
        DaoUtil tasmaniaUtil = new DaoUtil();
        tasmaniaUtil.getConnection();
    }
}

我已将所有登录详细信息存储在属性文件中。

我的第一个问题是如何将这两个 类 结合起来以合并这两个连接?我的第二个问题是如何使用 select 语句从 SQL 服务器获取所有数据,然后使用插入语句插入 Oracle 数据库?

SQL Server 和 Oracle 中的表(从 SQL Server 到 Oracle)

1.对于 MATERIAL_BATCH:

在SQL服务器中:

MATERIAL NUMBER|BATCH NUMBER|VENDOR BATCH NUMBER|VENDOR NUMBER|EXPIRATION DATE|
MODIFIED_DATETIME 

在SQL开发者:

MATERIAL NUMBER|BATCH NUMBER|VENDOR BATCH NUMBER|VENDOR NUMBER|GOODS_SUPPLIER_NUMBER|
EXPIRATION DATE|INSTIME 

2。对于 MATERIAL_MASTER:

在SQL服务器中:

PLANT|MATERIAL_NUMBER|MATERIAL_DESC|MODIFIED_DATETIME 

在SQL开发者:

PLANT|MATERIAL_NUMBER|MATERIAL_DESC|PROFIT_CENTER_NAME|STATUS|INSTIME  

3。对于供应商:

在SQL服务器中:

VENDOR_NUMBER|VENDOR_NAME|MODIFIED_DATETIME 

在SQL开发者:

VENDOR_NUMBER|VENDOR_NAME|VENDOR_LOCATION|INSTIME

有些字段在 SQL 服务器中没有,但在 SQL 开发人员 (Oracle) 中。对于那些我会保留 NULL.

这里有一些关于如何继续的草拟代码 - 由于缺少某些方法,它不会编译 - 但你应该能够从这里填补空白。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBCopy {

    public static void main(String[] args) {

        try {
            //You know how to do this - just rename and copy in...
            Connection sourceCon = getOracleConnection(); 
            Connection targetCon = geSqlServerConnection();

            copyMaterialBatch(sourceCon, targetCon);
            copyMaterialMaster(sourceCon, targetCon);
            copyVendor(sourceCon, targetCon);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void copyMaterialBatch(Connection sourceCon, Connection targetCon) throws SQLException {
        //Try-with-Resource to close all cursors once we're done
        try(//SELECT from source
                PreparedStatement ps = sourceCon.prepareStatement(
                    "SELECT \"MATERIAL NUMBER\", "
                    + "\"BATCH NUMBER\", "
                    + "\"VENDOR BATCH NUMBER\", "
                    + "\"VENDOR NUMBER\", "
                    + "\"EXPIRATION DATE\", "
                    + "\"MODIFIED_DATETIME\" FROM MATERIAL_BATCH");
            //INSERT into target
                PreparedStatement ins = targetCon.prepareStatement(
                        "INSERT INTO MATERIAL_BATCH([MATERIAL NUMBER], "
                    + "[BATCH NUMBER], "
                    + "[VENDOR BATCH NUMBER], "
                    + "[VENDOR NUMBER], "
                    + "[GOODS_SUPPLIER_NUMBER], "
                    + "[EXPIRATION DATE]"
                    + "[INSTIME]) VALUES (?,?,?,?,NULL,?,?)");
            //Perform select / open Cursor
                ResultSet rs = ps.executeQuery()) {
            int batchnr = 0;
            int MAXBATCH = 100;
            while(rs.next()) {
                //Set into INSERT the values we SELECTEd
                ins.setInt(1, rs.getInt("MATERIAL NUMBER")); 
                ins.setInt(2, rs.getInt("BATCH NUMBER")); 
                ins.setInt(3, rs.getInt("VENDOR BATCH NUMBER")); 
                ins.setInt(4, rs.getInt("VENDOR NUMBER"));              
                ins.setTimestamp(5, rs.getTimestamp("EXPIRATION DATE")); 
                ins.setTimestamp(6, rs.getTimestamp("MODIFIED_DATETIME"));
                //Add to Batch (you could executeUpdate here but if you have los of rows...)
                ins.addBatch();
                if(++batchnr % MAXBATCH == 0) {
                    ins.executeBatch();
                }
            }
            //if number of rows was not aligned on MAXBATCH size...
            ins.executeBatch();                     
        }                   
    }
}

请注意,Oracle 和 SQL-Server 对包含空格的列名使用不同的转义。 Oracle 需要 "COLUMN NAME" 而 SQL-Server 需要 [COLUMN NAME]

祝你好运。

编辑

适配你的真实参数类型:

//Set into INSERT the values we SELECTEd
            ins.setString(1, rs.getString("MATERIAL_NUMBER")); 
            ins.setString(2, rs.getString("BATCH_NUMBER")); 
            ins.setString(3, rs.getString("VENDOR_BATCH_NUMBER")); 
            ins.setString(4, rs.getString("VENDOR_NUMBER"));      
            ins.setString(5, rs.getString("GOODS_SUPPLIER_NUMBER"));
            //6th value is always null as specified in INSERT
            ins.setTimestamp(6, rs.getTimestamp("MODIFIED_DATETIME"));

所以现在我收到这个错误:列名 GOODS_SUPPLIER_NUMBER 无效。因此,这是将列为空的列之一,因为该列在源连接中不存在。以下是获取商品供应商编号的代码。我知道我非常接近。对此有什么想法吗? 谢谢桑尼

  private static void copyMaterialBatch(Connection sourceCon, Connection targetCon) throws SQLException {
        //Try-with-Resource to close all cursors once we're done
        try(//SELECT from source
                PreparedStatement ps = sourceCon.prepareStatement(
                    "SELECT [MATERIAL_NUMBER], "
                    + " [BATCH_NUMBER], "
                    + " [VENDOR_BATCH_NUMBER], "
                    + " [VENDOR_NUMBER], "
                    + " [EXPIRATION_DATE], "
                    + " [MODIFIED_DATETIME]  FROM dbo.MATERIAL_BATCH_VIEW");
            //INSERT into target
                PreparedStatement ins = targetCon.prepareStatement(
                        "INSERT INTO MATERIAL_BATCH(MATERIAL_NUMBER, "
                    + "BATCH_NUMBER, "
                    + "VENDOR_BATCH_NUMBER, "
                    + "VENDOR_NUMBER, "
                    **+ "GOODS_SUPPLIER_NUMBER, "**
                    + "EXPIRATION_DATE"
                    + "INSTIME) VALUES (?,?,?,?,NULL,?,?)");
            //Perform select / open Cursor
                ResultSet rs = ps.executeQuery()) {
            int batchnr = 0;
            int MAXBATCH = 1000;
            while(rs.next()) {
                //Set into INSERT the values we SELECTEd
                ins.setString(1, rs.getString("MATERIAL_NUMBER")); 
                ins.setString(2, rs.getString("BATCH_NUMBER")); 
                ins.setString(3, rs.getString("VENDOR_BATCH_NUMBER")); 
                ins.setString(4, rs.getString("VENDOR_NUMBER"));      
                ins.setString(5, rs.getString("GOODS_SUPPLIER_NUMBER"));
                ins.setString(6, rs.getString("EXPIRATION_DATE")); 
                ins.setTimestamp(7, rs.getTimestamp("MODIFIED_DATETIME"));
                //Add to Batch (you could executeUpdate here but if you have los of rows...)
                ins.addBatch();
                if(++batchnr % MAXBATCH == 0) {
                    ins.executeBatch();
                }
            }

所以当我删除 GOODS_SUPPLIER_NUMBER:

后,我的插入语句现在看起来像这样完整
try(//SELECT from source
                    PreparedStatement ps = sourceCon.prepareStatement(
                        "SELECT [PLANT], "
                        + "[MATERIAL_NUMBER], "
                        + "[MATERIAL_DESC], "
                        + "[MODIFIED_DATETIME] FROM dbo.MATERIAL_MASTER_VIEW");
                //INSERT into target
                    PreparedStatement ins = targetCon.prepareStatement(
"INSERT INTO MATERIAL_BATCH(MATERIAL_NUMBER, "
                + "BATCH_NUMBER, "
                + "VENDOR_BATCH_NUMBER, "
                + "VENDOR_NUMBER, "
                + "EXPIRATION_DATE"
                    + "INSTIME) VALUES (?,?,?,?,?,?)");
            //Perform select / open Cursor
                ResultSet rs = ps.executeQuery()) {
            int batchnr = 0;
            int MAXBATCH = 1000;
            while(rs.next()) {
                //Set into INSERT the values we SELECTEd
                ins.setString(1, rs.getString("MATERIAL_NUMBER")); 
                ins.setString(2, rs.getString("BATCH_NUMBER")); 
                ins.setString(3, rs.getString("VENDOR_BATCH_NUMBER")); 
                ins.setString(4, rs.getString("VENDOR_NUMBER"));      
                ins.setString(5, rs.getString("EXPIRATION_DATE")); 
                ins.setTimestamp(6, rs.getTimestamp("MODIFIED_DATETIME"));

所以现在插入语句中有 6 个值与 ins.setString 部分中的 6 个值匹配。

我得到的错误是:java.sql.BatchUpdateException: ORA-00913: 值太多

我认为最后一列 INSTIME 与 MODIFIED_DATETIME 不匹配。

我将再次确保将此答案归功于您。我只是不想来回修改代码,所以我把它保存在我回答自己问题的地方。再次感谢。

非常感谢。那奏效了。还有一件事。所以我有 Material Batch,Material Master 和 Vendor。出于某种原因,我收到供应商的错误。错误是:无效的列名 'VENDOR_NAME' 这是供应商的代码:

      private static void copyVendor(Connection sourceCon, Connection targetCon) throws SQLException {
            //Try-with-Resource to close all cursors once we're done
            try(//SELECT from source
                    PreparedStatement ps = sourceCon.prepareStatement(
                        "SELECT [VENDOR_NUMBER], "
                        + "[VENDOR_NAME], "
                        + "[MODIFIED_DATETIME] FROM dbo.VENDOR_VIEW");
                //INSERT into target
                    PreparedStatement ins = targetCon.prepareStatement(
                            "INSERT INTO VENDOR(VENDOR_NUMBER, "
                        + "VENDOR_NAME, "
                        + "INSTIME) VALUES (?,?,?)");
                //Perform select / open Cursor
                    ResultSet rs = ps.executeQuery()) {
                int vendornr = 0;
                int MAXVENDOR = 1000;
                while(rs.next()) {
                    //Set into INSERT the values we SELECTEd
                    ins.setString(1, rs.getString("VENDOR_NUMBER")); 
                    ins.setString(2, rs.getString("VENDOR_NAME")); 
                    ins.setTimestamp(3, rs.getTimestamp("MODIFIED_DATETIME"));
                    //Add to Batch (you could executeUpdate here but if you have los of rows...)
                    ins.addBatch();
                    if(++vendornr % MAXVENDOR == 0) {
                        ins.executeBatch();
                    }
                }
                //if number of rows was not aligned on MAXBATCH size...
                ins.executeBatch();                     
            }                   


    }

看不到我在这里遗漏了什么。我有三列,VENDOR_NUMBER、VENDOR_NAME 和 MODIFIED_DATETIME。我知道拼写是正确的。这更像是 'second set of eyes' 的情况。哈哈。再次感谢