ResultSet 还不为空 resultset.next() returns false
ResultSet is not null yet resultset.next() returns false
我正在尝试根据特定条件搜索表格。 SQL 在 SQL Developer 中执行时查询 returns 正确的结果(1 行)。在 JDBC 中,结果集还不为空 resultset.next()
returns false。
代码:
public static ArrayList<SearchRecord> searchRecords(Connection conn, int orderID) throws SQLException {
System.out.println("Order_id: " + orderID); //The correct orderID is passed
ArrayList<SearchRecord> searchResult = null;
SearchRecord searchRecord = null;
PreparedStatement search_pstm = null;
ResultSet search_rs = null;
if (conn != null) {
// This query is successfully executed in SQL Developer and outputs a row
String search_sql = "select d.PPS_NUM, a.BRC_ORD_ID, PROJECT_NUM, LICENSE_NUM, EXPIRY_DATE, FIRST_NAME, LAST_NAME, ADDRESS1, ADDRESS2, CITY, PROVINCE, POSTAL_CODE from VD_SHIP_AOR a, VD_BRC_ORD b, VD_CONSOL_VALID c, VD_SHIP d where a.brc_ord_id = b.brc_ord_id and b.CONSOL_VALID_ID= c.consol_valid_id and a.SHIP_ID = d.SHIP_ID and d.ORD_ID = b.ORD_ID and d.PPS_NUM = ?";
search_pstm = conn.prepareStatement(search_sql);
search_pstm.setInt(1, orderID);
search_rs = search_pstm.executeQuery();
searchResult = new ArrayList<SearchRecord>();
if (search_rs != null) {
System.out.println("Not null"); //Prints this
System.out.println(search_rs.isClosed()); //Prints false
System.out.println(search_rs.getRow()); //Prints 0
while (search_rs.next()) { //Does not enter the while loop, why?
System.out.println("Inside while rs.next");
//store resultset's data into arraylist
}
}
}
return searchResult;
}
我认为参考的内容:
Similar Stack Overflow question
Check for emptyResultSet
但是现有的解决方案对我不起作用,请帮助我解决这个问题。
您似乎假设此方法 returns null
如果没有行,情况并非如此。方法 executeQuery()
将 永远不会 return null
*。 API 文档特别指出:
Returns:
a ResultSet
object that contains the data produced by the query; never null
因此,您的 null
-检查完全没有必要。它要么产生一个结果集(即使查询没有产生任何行),要么它会抛出一个 SQLException
例如语法错误,或者如果执行的语句没有产生结果集(例如更新或删除陈述)。
执行后的结果集最初是打开的,位于第一行(如果有)之前。如果没有更多行,则调用 ResultSet.next()
returns false
。在这种情况下,您的 select 语句可能没有产生任何行,因此结果集为空并且第一次调用 next()
将 return false
.
我认为您对 API 的假设是不正确的。我建议你仔细阅读 JDBC API 文档,也许可以阅读 JDBC.
上的一些教程或介绍性文本
如果 SQL Developer 显示一行而您的代码没有,请仔细检查 1) 您是否真的使用相同的数据库,2) 使用的 id 是否真的相同,以及 3) 如果您在 SQL 中查看的数据实际上是开发人员提交的。并考虑首先简化您的查询,例如 select 仅来自一个 table,删除此订单号上的特定条件,以便您 select 所有行,使用与 [=] 中使用的相同的文字值56=] 开发人员(在查询中硬编码)等。另外,您确定您使用的条件正确吗,PPS_NUM
看起来不像 Order_id
,而例如 ORD_ID
看起来更像一个。
* : 忽略特定驱动程序实现中出现错误的可能性
我正在尝试根据特定条件搜索表格。 SQL 在 SQL Developer 中执行时查询 returns 正确的结果(1 行)。在 JDBC 中,结果集还不为空 resultset.next()
returns false。
代码:
public static ArrayList<SearchRecord> searchRecords(Connection conn, int orderID) throws SQLException {
System.out.println("Order_id: " + orderID); //The correct orderID is passed
ArrayList<SearchRecord> searchResult = null;
SearchRecord searchRecord = null;
PreparedStatement search_pstm = null;
ResultSet search_rs = null;
if (conn != null) {
// This query is successfully executed in SQL Developer and outputs a row
String search_sql = "select d.PPS_NUM, a.BRC_ORD_ID, PROJECT_NUM, LICENSE_NUM, EXPIRY_DATE, FIRST_NAME, LAST_NAME, ADDRESS1, ADDRESS2, CITY, PROVINCE, POSTAL_CODE from VD_SHIP_AOR a, VD_BRC_ORD b, VD_CONSOL_VALID c, VD_SHIP d where a.brc_ord_id = b.brc_ord_id and b.CONSOL_VALID_ID= c.consol_valid_id and a.SHIP_ID = d.SHIP_ID and d.ORD_ID = b.ORD_ID and d.PPS_NUM = ?";
search_pstm = conn.prepareStatement(search_sql);
search_pstm.setInt(1, orderID);
search_rs = search_pstm.executeQuery();
searchResult = new ArrayList<SearchRecord>();
if (search_rs != null) {
System.out.println("Not null"); //Prints this
System.out.println(search_rs.isClosed()); //Prints false
System.out.println(search_rs.getRow()); //Prints 0
while (search_rs.next()) { //Does not enter the while loop, why?
System.out.println("Inside while rs.next");
//store resultset's data into arraylist
}
}
}
return searchResult;
}
我认为参考的内容:
Similar Stack Overflow question
Check for emptyResultSet
但是现有的解决方案对我不起作用,请帮助我解决这个问题。
您似乎假设此方法 returns null
如果没有行,情况并非如此。方法 executeQuery()
将 永远不会 return null
*。 API 文档特别指出:
Returns:
aResultSet
object that contains the data produced by the query; nevernull
因此,您的 null
-检查完全没有必要。它要么产生一个结果集(即使查询没有产生任何行),要么它会抛出一个 SQLException
例如语法错误,或者如果执行的语句没有产生结果集(例如更新或删除陈述)。
执行后的结果集最初是打开的,位于第一行(如果有)之前。如果没有更多行,则调用 ResultSet.next()
returns false
。在这种情况下,您的 select 语句可能没有产生任何行,因此结果集为空并且第一次调用 next()
将 return false
.
我认为您对 API 的假设是不正确的。我建议你仔细阅读 JDBC API 文档,也许可以阅读 JDBC.
上的一些教程或介绍性文本如果 SQL Developer 显示一行而您的代码没有,请仔细检查 1) 您是否真的使用相同的数据库,2) 使用的 id 是否真的相同,以及 3) 如果您在 SQL 中查看的数据实际上是开发人员提交的。并考虑首先简化您的查询,例如 select 仅来自一个 table,删除此订单号上的特定条件,以便您 select 所有行,使用与 [=] 中使用的相同的文字值56=] 开发人员(在查询中硬编码)等。另外,您确定您使用的条件正确吗,PPS_NUM
看起来不像 Order_id
,而例如 ORD_ID
看起来更像一个。
* : 忽略特定驱动程序实现中出现错误的可能性