【问题标题】:cannot exexute SQL query wirh OJDBC and Oracle DB (but full working with posgtres JDBC and Postgres DB)无法使用 OJDBC 和 Oracle DB 执行 SQL 查询(但完全可以使用 postgres JDBC 和 Postgres DB)
【发布时间】:2016-05-01 23:39:21
【问题描述】:

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

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

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

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

这是我的数据库对象和测试查询

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;
}

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

非常感谢

【问题讨论】:

  • 您正在吞食executeCommand 中的异常。如果那里发生异常,rs 将保持null。您可能想要记录或打印异常(或抛出异常,因为此代码无法处理它们)。

标签: java mysql oracle postgresql jdbc


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2020-12-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多