无法通过OJDBC和Oracle DB执行SQL查询(但完全使用posgtres JDBC和Postgres DB)

时间:2016-01-25 15:35:39

标签: java mysql oracle postgresql jdbc

我写了一个java程序,它从PG gb中检索数据,处理它们,并将它们写在Oracle DB中。

虽然PG部分完全正常工作,但Oracle部分存在问题。

我可以连接到数据库,但每个查询都以回滚结束(带有Oracle的ResultSet始终为null)

当然我有PG和Oracle JDBC驱动程序。

这是我的DBs对象和测试查询

private final static PostgresDB postgres = new PostgresDB("jdbc:postgresql://192.168.2.23:5432/T18CLEAN", "myPGUser", "myPGPasswd", true);
private final static OracleDB oracle = new OracleDB("jdbc:oracle:thin:@192.168.2.20:1521/EFFEVI.T18FV.IT", "myOracleUser", "myOraclePasswd");
private final static String testPostgres = "SELECT product_pricelist_item.x_product_name FROM public.product_pricelist_item;";
private final static String testOracle = "SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI;";

然后我设置了2个连接:

PG:

    public Connection getConnect() throws ClassNotFoundException {
    System.out.println("-------- Posgres JDBC Connection Testing ------");
    String url = c_url;
    Connection conn = null;
    Properties props = new Properties();
    props.setProperty("user", user);
    props.setProperty("password", passwd);
    props.setProperty("ssl", boolToString(sslEnabled));
    try{
        Class.forName("org.postgresql.Driver");
        System.out.println("Postgres JDBC Driver Registered!");
    } catch(ClassNotFoundException e) {
        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return null;
    }

    try {
        conn = DriverManager.getConnection(url, props);
        System.out.println("You made it, take control your Postgres database now!");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Failed to make connection to Postgres DB!");
    }
    return conn;
}

甲骨文:

    public Connection getConnect(){
    Connection connection = null;
    System.out.println("-------- Oracle JDBC Connection Testing ------");

    try {

        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {

        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return connection;

    }

    System.out.println("Oracle JDBC Driver Registered!");



    try {

        connection = DriverManager.getConnection(c_url, user, passwd);

    } catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return connection;

    }

    if (connection != null) {
        System.out.println("You made it, take control your Oracle database now!");
        return connection;
    } else {
        System.out.println("Failed to make connection to Oracle DB!");
    }

    return connection;
}

在所有这些传递之后我执行查询

    public ResultSet executeCommand(Connection c, String command) {
    Statement st = null;
    ResultSet rs = null;
    try {
        st = c.createStatement();
        rs = st.executeQuery(command);
    } catch (SQLException e) {

    }

    if(rs==null){
        System.out.println("Failed to Execute command " + command);
    } else {
        System.out.println("Command Executed: " + command);
    }

    return rs;
}

假设没有参数错误......可能是什么?有什么帮助吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

在查询结尾处删除分号。

使用此:

private final static String testOracle = 
"SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI";

而不是这一个:

private final static String testOracle = 
"SELECT EFFEVI.PRESA_ORDINI.PO_CLIENTE FROM EFFEVI.PRESA_ORDINI;";



也不要在代码中默默地“吞下”异常:

} catch (SQLException e) {

}

重新抛出异常,或者至少将错误打印到日志中:

} catch (SQLException e) {
  log.error("Error while executing query " + command, e);
  throw new RuntimeException("Error while executing query " + command, e);
}