简体   繁体   English

JDBC ResultSet无法存储查询结果

[英]JDBC ResultSet fails to store query results

I am currently trying to run a query on an Oracle database through the JDBC. 我目前正在尝试通过JDBC在Oracle数据库上运行查询。 I have tested my queries in SQLDeveloper, but when I try to run the queries within my Java program, my ResultSet rs.next() result returns false (meaning there isn't anything stored in rs). 我已经在SQLDeveloper中测试了查询,但是当我尝试在Java程序中运行查询时,我的ResultSet rs.next()结果返回false(意味着rs中没有存储任何内容)。

My code is below: 我的代码如下:

public static void testFunction() {
    Properties properties = new Properties();
    properties.put("user", "USERNAMEHERE");
    properties.put("password", "PASSWORDHERE");

    String URL = "jdbc:oracle:thin:@abc:123:def456";
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String sqltxt = "SELECT a.MESSAGE, a.DATE, a.ID_NUM, b.MESSAGE, b.ANOTHER_ID FROM USER.SOME_TABLE_NAME a INNER JOIN USER.DIFFERENT_TABLE_NAME b on a.MESSAGE = b.MESSAGE where a.DATE= '1-December-2014' and b.ANOTHER_ID = 3 and a.ID_NUM IN(0, 100)";


    try {
        conn = DriverManager.getConnection(URL, properties);
        stmt = conn.prepareStatement(sqltxt, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery();
        System.out.println("Records exist? " + rs.next());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try { if (rs != null) rs.close(); } catch (Exception e) {};
        try { if (stmt != null) stmt.close(); } catch (Exception e) {};
        try { if (conn != null) conn.close(); } catch (Exception e) {};
    }
}

I have noticed that whenever I add a WHERE clause to sqltxt, that is when my rs.next() yields false. 我注意到,每当我在sqltxt中添加WHERE子句时,即rs.next()产生false时。

Any insight is appreciated, if I happen to find the solution myself, I will post it here. 感谢您提供任何见识,如果我碰巧自己找到解决方案,我将在此处发布。

EDIT: Here is the same code from above BUT with a different query for sqltxt: 编辑:这是来自BUT的相同代码,但对sqltxt的查询不同:

public static void testFunction() {
    Properties properties = new Properties();
    properties.put("user", "USERNAMEHERE");
    properties.put("password", "PASSWORDHERE");

    String URL = "jdbc:oracle:thin:@abc:123:def456";
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String sqltxt = "SELECT MESSAGE FROM USER.SOME_TABLE WHERE DATE = '01-December-2015'";

    try {
        conn = DriverManager.getConnection(URL, properties);
        stmt = conn.prepareStatement(sqltxt, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery();
        System.out.println("Records exist? " + rs.next());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try { if (rs != null) rs.close(); } catch (Exception e) {};
        try { if (stmt != null) stmt.close(); } catch (Exception e) {};
        try { if (conn != null) conn.close(); } catch (Exception e) {};
    }
}

You should avoid hard-coded dates in your SQL text. 您应该避免在SQL文本中使用硬编码的日期。 Especially with Oracle, it requires a lot of work. 特别是对于Oracle,它需要大量工作。 Here are two answers, one using a hard-coded date, another which properly utilizes the power of your PreparedStatement object: 这是两个答案,一个答案使用硬编码日期,另一个答案正确利用了PreparedStatement对象的功能:

Date in-line : 在线日期

public static void testFunction() {
    Properties properties = new Properties();
    properties.put("user", "USERNAMEHERE");
    properties.put("password", "PASSWORDHERE");

    String URL = "jdbc:oracle:thin:@abc:123:def456";
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    String sqltxt = "SELECT MESSAGE FROM USER.SOME_TABLE WHERE DATE = to_date('01-December-2015', 'dd-month-yyyy')";

    try (Connection conn = DriverManager.getConnection(URL, properties);
        PreparedStatement stmt = conn.prepareStatement(sqltxt, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = stmt.executeQuery();) {

        System.out.println("Records exist? " + rs.next());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

(see: http://www.techonthenet.com/oracle/functions/to_date.php for more information about hard-coding dates in Oracle) (有关Oracle中硬编码日期的更多信息,请参见: http : //www.techonthenet.com/oracle/functions/to_date.php

Date through the Parameterized SQL : 通过参数化SQL的日期

public static void testFunction() {
    Properties properties = new Properties();
    properties.put("user", "USERNAMEHERE");
    properties.put("password", "PASSWORDHERE");

    String URL = "jdbc:oracle:thin:@abc:123:def456";
    String sqltxt = "SELECT MESSAGE FROM USER.SOME_TABLE WHERE DATE = ?";

    try (Connection conn = DriverManager.getConnection(URL, properties);
        PreparedStatement stmt = conn.prepareStatement(sqltxt, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = stmt.executeQuery();) {

        stmt.setDate(1, Date.valueOf(LocalDate.now().withYear(2015).withMonth(12).withDayOfMonth(1)));
        System.out.println("Records exist? " + rs.next());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

read: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html for more information about using Parameterized SQL in Java. 请阅读: http : //docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html ,以获取有关在Java中使用参数化SQL的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM