无法从Java ResultSet

时间:2017-08-23 18:31:26

标签: java resultset

我正在尝试访问从Java ResultSet对象获取的一些数据。

Context ctx = new InitialContext();
DataSource datasource = ctx.lookup("database")

PreparedStatement query = datasource.getConnection().prepareStatement( "select * \n" +
                    "from appDb.market a \n" +
                    "where not exists \n" +
                    "(select * from mainDb.market b where a.id = b.id);");

    ResultSet rs = query.executeQuery();

            if (rs.next()){
                //The code will not reach this condition
                System.out.println("Data found!");
            }

但是,由于某种原因,rs.next()没有评估为true,即使当前,ResultSet的rowData也有行..

我可以在调试工具中访问结果集的rowData对象,并且可以看到行的大小是1,但是,我无法访问所述行。我想我必须做些傻事,但不太确定。

1 个答案:

答案 0 :(得分:0)

这就是documentation所读的内容:

  

如果新的当前行有效,则为true;如果没有更多行,则为false

在你的情况下,由于没有更多的行,它应该返回false。

此外,您可能希望使用first()代替条件用例。