JAVA JDBC SQL基于旧数据库记录

时间:2015-07-02 16:40:37

标签: java oracle jdbc database-connection informix

我正在尝试编写一个可运行的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;
 }
}

1 个答案:

答案 0 :(得分: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()