2015-07-02 163 views
0

我正在嘗試編寫一個可以連接到2個不同數據庫的可運行jar文件informix舊數據庫和oracle新數據庫。它應該能夠用舊數據庫(informix)記錄更新新數據庫(oracle)。JAVA JDBC SQL基於舊數據庫記錄更新新數據庫

我重新編輯我的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; 
} 
} 
+0

你不能像這樣做。您需要兩條語句,每條語句一個,並自行編寫數據傳輸。 –

+0

@MarkRotteveel - 公平地說,如果設置了聯合(如Oracle透明網關或InfoSphere Federation Server),則可以在單個查詢中訪問兩個不同的數據源。 – mustaccio

+0

@mustaccio真的,但在這種情況下,它不會真正與JDBC相關,但與所用系統的確切語法更相關。該代碼顯示了兩種不同的連接,使我可以採用純粹的JDBC解決方案。 –

回答

0

首先嚐試從源數據庫中的數據導出到文本文件。

您的代碼使用硬編碼的列名,但我認爲它可以讀取表名以從SELECT * FROM [table_name]的元數據中導出某些配置文件和列名。在JDBC中,RecordSet有getMetaData()。用它。

將數據導出到文本文件時沒有問題時,可以執行下一步:將這些數據直接從源數據庫導入到目標數據庫。

對於目標數據庫中創建prepareStatement有:

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

(有question_marks是映射到列字符 '?')。

然後從源表中的每個記錄,每個記錄(行)做:

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

對於大表,你也可以使用setFetchSize()addBatch()/executeBatch()