使用JDBC将表从SQL Server复制到Oracle

时间:2015-11-16 16:28:20

标签: java mysql sql-server jdbc oracle-sqldeveloper

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

SQL Server连接:

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 Server中获取所有数据,然后将insert语句插入到Oracle数据库中?

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

1。对于MATERIAL_BATCH:

在SQL Server中:

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

在SQL Developer中:

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

2。对于MATERIAL_MASTER:

在SQL Server中:

PLANT|MATERIAL_NUMBER|MATERIAL_DESC|MODIFIED_DATETIME 

在SQL Developer中:

PLANT|MATERIAL_NUMBER|MATERIAL_DESC|PROFIT_CENTER_NAME|STATUS|INSTIME  

第3。对于供应商:

在SQL Server中:

VENDOR_NUMBER|VENDOR_NAME|MODIFIED_DATETIME 

在SQL Developer中:

VENDOR_NUMBER|VENDOR_NAME|VENDOR_LOCATION|INSTIME

SQL Server(Oracle)中有一些SQL Server中没有的字段。对于那些我将保留NULL

2 个答案:

答案 0 :(得分:3)

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

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"));

答案 1 :(得分:-1)

所以现在我收到此错误:列名GOODS_SUPPLIER_NUMBER无效。因此,这是列为null的列之一,因为源连接中不存在此列。以下是提货商品编号的代码。我知道我非常接近。关于这个的任何想法? 谢谢Sonny

  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"));

所以现在insert语句中有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。我知道拼写是正确的。这更像是“第二眼”的情况。大声笑。再次感谢