【问题标题】:JAVA JDBC SQL updating a new database based on the old database recordsJAVA JDBC SQL 根据旧数据库记录更新新数据库
【发布时间】:2015-09-20 07:33:02
【问题描述】:

我正在尝试编写一个可运行的 jar 文件,它可以连接到 2 个不同的数据库,informix 旧数据库和 oracle 新数据库。它应该能够用旧的数据库(informix)记录更新新的数据库(oracle)。

我重新编辑了我的 java 代码我为我的选择、更新和连接添加了单独的方法我没有收到错误但它没有更新我的数据库。我的选择有效,但我的更新语句不起作用。这是我得到的结果 - SELECT profile_id, ingress_flag, egress_flag, ce_ingress_flag, ce_egress_flag from COS_PROFILE where profile_id = 102 profileid : 102 ingressflag : Y egress_flag : Y ceingressflag : Y ceegressflag : Y ResultSet not open, operation 'next' not permitted. Verify that autocommit is OFF

我不确定如何修复ResultSet not open, operation 'next' not permitted. Verify that autocommit is OFF

public class TestConnection {

static ResultSet rs;

public static void main (String[] args) throws Exception  {


    try{
        selectRecordsIcore();
        updateRecordIntoBids();
    }  catch (SQLException e) {

        System.out.println(e.getMessage());
    }

}

public static void selectRecordsIcore() throws SQLException {

    Connection dbConnection = null;
    Statement statement = null;

    String selectTableSQL = "SELECT profile_id, ingress_flag, egress_flag, ce_ingress_flag, ce_egress_flag from COS_PROFILE";


    try {
        dbConnection = getInformixConnection();
        statement = dbConnection.createStatement();

         System.out.println(selectTableSQL);

        // execute select SQL stetement
         rs = statement.executeQuery(selectTableSQL);

                while (rs.next()) {

                int profileid = rs.getInt("profile_id");
                String ingressflag = rs.getString("ingress_flag");
                String egress_flag = rs.getString("egress_flag");
                String ceingressflag = rs.getString("ce_ingress_flag");
                String ceegressflag = rs.getString("ce_egress_flag");

                System.out.println("profileid : " + profileid);
                System.out.println("ingressflag : " + ingressflag);
                System.out.println("egress_flag : " + egress_flag);
                System.out.println("ceingressflag : " + ceingressflag);
                System.out.println("ceegressflag : " + ceegressflag);


            }

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    } finally {

        if (statement != null) {
            statement.close();
        }

        if (dbConnection != null) {
            dbConnection.close();
        }

    }

}


private static void updateRecordIntoBids() throws SQLException {

    Connection dbConnection = null;
    Statement statement = null;
    ArrayList<TempStorageRecords>  updateSQL = new  ArrayList<TempStorageRecords>();

    while (rs.next()) {

        int profileid = rs.getInt("profile_id");
        String ingressflag = rs.getString("ingress_flag");
        String egress_flag = rs.getString("egress_flag");
        String ceingressflag = rs.getString("ce_ingress_flag");
        String ceegressflag = rs.getString("ce_egress_flag");



            String updateTableSQL = "UPDATE traffic_profile SET ingress_flag  = " +  ingressflag
                 + " ,egress_flag = " + egress_flag
                 + " ,ce_ingress_flag = " + ceingressflag
                 + " ,ce_egress_flag = " + ceegressflag 
                 + " WHERE profile_id = " + profileid +  ";";

            try {
                dbConnection = getOracleConnection();
                statement = dbConnection.createStatement();

                System.out.println("updateTableSQL 1 :" + updateTableSQL);

                // execute update SQL stetement
                statement.execute(updateTableSQL);

                System.out.println("updateTableSQL 2: " + updateTableSQL);

            } catch (SQLException e) {

                System.out.println(e.getMessage());

            } finally {

                if (statement != null) {
                    statement.close();
                }

                if (dbConnection != null) {
                    dbConnection.close();
                }

            }   
    }

   }





public static Connection getOracleConnection() throws SQLException {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@oracle_host:1521:BIDS";
String username = "username";
String password = "password";
try {
    Class.forName(driver);
} catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} // load Oracle driver
Connection dbConnection = null;

try {

    Class.forName(driver);

} catch (ClassNotFoundException e) {

    System.out.println(e.getMessage());

}

try {

    dbConnection = DriverManager.getConnection(
            url, username,password);
    return dbConnection;

} catch (SQLException e) {

    System.out.println(e.getMessage());

}

return dbConnection;


 }

public static Connection getInformixConnection() throws SQLException {
    String driver = "com.informix.jdbc.IfxDriver";
    String url = "jdbc:informix-sqli://informix_host:1615/icore:INFORMIXSERVER=icit";
    String username = "user";
    String password = "pass";
    try {
        Class.forName(driver);
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } // load Informix driver
    Connection dbConnection = null;

    try {

        Class.forName(driver);

    } catch (ClassNotFoundException e) {

        System.out.println(e.getMessage());

    }

    try {

        dbConnection = DriverManager.getConnection(
                url, username,password);
        return dbConnection;

    } catch (SQLException e) {

        System.out.println(e.getMessage());

    }

    return dbConnection;
 }
}

【问题讨论】:

  • 你不能这样。您将需要两个语句,一个用于每个数据库,并自己编程数据传输。
  • @MarkRotteveel -- 公平地说,如果您设置了联邦,例如 Oracle 透明网关或 InfoSphere Federation Server,您可以在一个查询中访问两个不同的数据源。
  • @mustaccio 是的,但在这种情况下,它不会真正与 JDBC 相关,而是与所使用系统的确切语法相关。该代码显示了两个不同的连接,导致我假设一个纯 JDBC 解决方案。
  • 有什么简单的方法吗?
  • 是的,有。从一个数据库中读取数据并将其存储到另一个数据库中。

标签: java oracle jdbc database-connection informix


【解决方案1】:

首先尝试将数据从源数据库导出到文本文件中。

您的代码使用硬编码的列名,但我认为它可以从SELECT * FROM [table_name] 的元数据中读取一些配置文件中导出的表名和列名。在 JDBC 中有 getMetaData() 用于 RecordSet。使用它。

当您将数据导出到文本文件中没有问题时,您可以执行下一步:将此类数据直接从源数据库导入目标数据库。

为目标数据库创建prepareStatement :

'INSERT INTO ' + table_name_dest + ' (' + column_names +') VALUES ('+ question_marks + ')'

(question_marks 是映射到列的“?”字符)。

然后对源表中的每条记录和每条记录(行)执行:

insert_stmt.setObject(i, rs_in.getObject(i))

对于大表,您还可以使用setFetchSize() 和addBatch()/executeBatch()

【讨论】:

    猜你喜欢
    • 2021-09-14
    • 2016-01-15
    • 2020-12-12
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多