JDBC ResultSet 存储查询结果失败
JDBC ResultSet fails to store query results
我目前正在尝试 运行 通过 JDBC 查询 Oracle 数据库。我已经在 SQLDeveloper 中测试了我的查询,但是当我尝试 运行 我的 Java 程序中的查询时,我的 ResultSet rs.next() 结果 returns false (意思是没有' t 任何存储在 rs 中的内容)。
我的代码如下:
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) {};
}
}
我注意到每当我向 sqltxt 添加一个 WHERE 子句时,我的 rs.next() 就会返回 false。
感谢任何见解,如果我碰巧自己找到了解决方案,我会post在这里。
编辑:这是与上面相同的代码,但对 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) {};
}
}
您应该避免在 SQL 文本中硬编码日期。尤其是 Oracle,需要做大量的工作。这里有两个答案,一个使用硬编码日期,另一个正确地利用了 PreparedStatement
对象的力量:
在线日期:
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();
}
}
(有关 Oracle 中硬编码日期的更多信息,请参阅:http://www.techonthenet.com/oracle/functions/to_date.php)
通过参数化的日期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();
}
}
阅读:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 了解有关在 Java.
中使用参数化 SQL 的更多信息
我目前正在尝试 运行 通过 JDBC 查询 Oracle 数据库。我已经在 SQLDeveloper 中测试了我的查询,但是当我尝试 运行 我的 Java 程序中的查询时,我的 ResultSet rs.next() 结果 returns false (意思是没有' t 任何存储在 rs 中的内容)。
我的代码如下:
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) {};
}
}
我注意到每当我向 sqltxt 添加一个 WHERE 子句时,我的 rs.next() 就会返回 false。
感谢任何见解,如果我碰巧自己找到了解决方案,我会post在这里。
编辑:这是与上面相同的代码,但对 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) {};
}
}
您应该避免在 SQL 文本中硬编码日期。尤其是 Oracle,需要做大量的工作。这里有两个答案,一个使用硬编码日期,另一个正确地利用了 PreparedStatement
对象的力量:
在线日期:
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();
}
}
(有关 Oracle 中硬编码日期的更多信息,请参阅:http://www.techonthenet.com/oracle/functions/to_date.php)
通过参数化的日期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();
}
}
阅读:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 了解有关在 Java.
中使用参数化 SQL 的更多信息