将oracle表复制到DB2

时间:2011-09-22 06:48:50

标签: oracle db2 oracle11g db2-connect

我想访问DB2中的oracle表(类似于从DB2到oracle的DBlink)。感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

您可以使用db2联合。一个链接是here

答案 1 :(得分:1)

Oracle有一项称为异构服务的功能,它允许我们在Oracle数据库和非Oracle数据库(包括DB2)之间建立链接。 Find out more

答案 2 :(得分:0)

import com.ibm.db2.jcc.am.gc;
import com.ibm.db2.jcc.t2zos.s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import static org.omg.IOP.ENCODING_CDR_ENCAPS.value;

public class automateExport {

    static String value;

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // ResultSet rs = null;
        String table_name;
        Integer temp = 0;
        Integer temp1 = 0;
        Integer temp2 = 1;
        String column_name = null;
        String tableName = null;
        String columnType = null;
        int precision = 0;

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection codal = DriverManager.getConnection("jdbc:oracle:thin:@192.168.01.53:1521:orcl", "NAVID",
                "oracle");

        StringBuilder sb = new StringBuilder(1024);

        Connection DB2 = getConnection();
        String sql = "SELECT TABSCHEMA,TABNAME,COLNAME,TYPENAME,LENGTH FROM SYSCAT.COLUMNS WHERE TABSCHEMA NOT LIKE 'SYS%' ";
        PreparedStatement mainStmt = DB2.prepareStatement(sql);
        ResultSet rs = mainStmt.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        String str1 = "ADMIN2";

        while (rs.next()) {

            table_name = rs.getString(2);

            if (table_name.equalsIgnoreCase(str1)) {
                if (temp1 == 0) {

                    sb.append("create table").append(" ");
                    sb.append(table_name).append("( ");
                    if (temp2 == 0) {
                        sb.append(" ").append(column_name).append(" ").append(columnType);
                        if (precision != 0) {
                            sb.append("( ").append(precision).append(" )");
                            sb.append(", ");

                        }
                    }

                    temp1 = 1;
                    temp = 1;
                }

                if (temp == 0) {
                    sb.append(table_name).append("(");
                    temp = 1;
                }
                column_name = rs.getString(3);
                columnType = rs.getString(4);
                sb.append(" ").append(column_name).append(" ").append(columnType);
                precision = rs.getInt(5);
                if (precision != 0) {
                    sb.append("( ").append(precision).append(" )");
                    sb.append(", ");
                }
            } else {
                temp2 = 0;
                sb.replace(sb.length() - 2, sb.length(), "");
                sb.append(" )");
                temp1 = 0;
                str1 = str1.replaceAll(str1, table_name);
                column_name = rs.getString(3);
                columnType = rs.getString(4);
                precision = rs.getInt(5);
                String sql2 = sb.toString();
                PreparedStatement m = codal.prepareStatement(sql2);
                m.executeUpdate();
                sb.delete(0, sb.length());
            }
        }
        codal.close();
        DB2.close();
/*


        }


    private static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver");
        Connection connection
                = DriverManager.getConnection("jdbc:db2://localhost:50000/navid", "navid", "oracle");
        return connection;

    }

}