【发布时间】:2023-03-23 23:44:01
【问题描述】:
我正在尝试从 Oracle 数据库中提取结果。我编写了一个正确的查询,并且在 sqlplus 中手动发出时会产生准确的结果。此外,当查询仅匹配一行时,代码按预期工作(换句话说,当 ResultSet 只有一行时,一切正常)。但是,当多行匹配查询时,Oracle JDBC 返回的 ResultSet 为空。
public Component[] getAllComponents(int typeId, int osId) throws SQLException
{
String query= "SELECT c.component_id, c.component_name, c.component_version, c.type_id, c.post_download_instructions, "
+ "o.os_id, o.os_name, o.description AS os_description, "
+ "i.file_location, i.release_date, i.patch_number, i.file_id, "
+ "i.description AS i_description "
+ "FROM components c, installation_files i, operating_systems o "
+ "WHERE c.type_id = ? "
+ "AND i.os_id = ? "
+ "AND c.component_id = i.component_id "
+ "AND i.os_id = o.os_id";
ResultSet results = null;
PreparedStatement stmt = null;
ArrayList<Component> found = new ArrayList<Component>();
try {
stmt = dbConn.prepareStatement(query); //dbConn is member variable
stmt.setInt(1, typeId);
stmt.setInt(2, osId);
results = stmt.executeQuery();
while(results.next()){
//Some logic
}
} finally {
if(results != null) results.close();
if(stmt != null) stmt.close();
dbConn.close();
}
//More Code
//etc. etc.
检查 ResultSet 表明,当获取的结果应该包含多于一行时,调用 ResultSet.next() 永远不会产生true。但是,手动发出查询确实会产生结果,并且当只返回一行时,一切正常。有谁知道发生了什么?我正在使用 Oracle 的 ojdbc6.jar。
谢谢!
【问题讨论】:
-
也许您没有使用您认为正在使用的数据库?也许你有多个数据库,一个用于生产,一个用于测试环境,一个用于本地开发,等等。仔细检查 JDBC URL。或者您可能没有指定您认为指定的
typeId和/或osId?调试它们。 -
是的,我会仔细检查您的配置。这段代码似乎没问题。
-
您可以检查“从 DUAL 中选择 SYSDATE”。如果这有效,那么您的查询或参数有问题。
-
很难说发生了什么。看了你的代码,感觉还不错。请分享表格结构和数据。
-
如果删除所有的 '?' 会发生什么?条件?