简体   繁体   English

在PreparedStatement之后,非空数据库表的结果集为空

[英]Empty ResultSet for non-empty database table after PreparedStatement

I have written a query which retrieves data from a table and then I am checking the number of rows in the resultset using getRow() method. 我编写了一个查询,该查询从表中检索数据,然后使用getRow()方法检查结果集中的行数。 This is printing 0 ,however my table contains multiple row column data in it. 这是打印0 ,但是我的表中包含多个行列数据。

I checked previous questions here and found one much similar here . 我在这里检查了以前的问题,然后在这里发现了一个非常相似的问题 But unlike what is mentioned in that I do not have any permissions defined and connection to the database is well established. 但是与所提到的不同,我没有定义任何权限,并且与数据库的连接已经很好地建立了。 It is a very simple query returning updatable resultset as following: 这是一个非常简单的查询,返回可更新的结果集,如下所示:

if(temp == null) {
                query = "SELECT * FROM altemp1";
                pstmt = cn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                temp = pstmt.executeQuery();
                System.out.println("TEMP LENGTH : "+temp.getRow());
            }

temp is the ResultSet and altemp1 is the table in DB. tempResultSet,altemp1是DB中的 The check above if temp is null? 上面检查temp是否为null? is for some logical purpose. 是出于某种逻辑目的。

You need to call ResultSet.next() and check the return code to fetch each successive row, including the first, and you then need to call various other methods to get the columns, one of which is not getRow() . 您需要调用ResultSet.next()并检查返回码以获取每个连续的行(包括第一行),然后需要调用各种其他方法来获取列,其中之一不是 getRow() See the Javadoc . 参见Javadoc

According to the JavaDoc ( https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getRow%28%29 ): 根据JavaDoc( https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getRow%28%29 ):

[...getRow()] retrieves the current row number. [... getRow()]检索当前行号。 The first row is number 1, the second number 2, and so on. 第一行是数字1,第二行是数字2,依此类推。

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

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