-1

I'm attempting to access some data that I get from a Java ResultSet object.

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!");
            }

However, for some reason, rs.next() is not evaluating to true, even though currently, the ResultSet's rowData does have rows..

I can access the result set's rowData object in the debugging tools, and can see that the size of rows is 1, however, I cannot access said row. I think I must be doing something silly, but am not so sure.

Alexander Edwards
  • 772
  • 2
  • 7
  • 18
  • Try running the same query directly against the database, and see if you get results at all. The fact that you have an internal data structure which is not empty does not indicate that you have an actual row. – RealSkeptic Aug 23 '17 at 18:40
  • @RealSkeptic I've done this, and it does indeed return results. – Alexander Edwards Aug 23 '17 at 18:40
  • @RealSkeptic Could it be that it is only returning one row(which it is), and since I am only getting one row, that rs.next() will not evaluate to true? – Alexander Edwards Aug 23 '17 at 18:41
  • Nope. You can't get any result without calling `rs.next()` first. Perhaps you're not showing us the whole code. Do you have a `try` around that code? What does the `catch` include? Please [edit] your question and show the whole thing. – RealSkeptic Aug 23 '17 at 18:48
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Aug 23 '17 at 18:51
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Aug 23 '17 at 18:56

1 Answers1

0

That's what the documentation reads :

true if the new current row is valid; false if there are no more rows

In your case since there are no more rows, it should return false.

Also you might want to use first() instead for your conditional use-case.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • But the documentation also says that it starts _before_ the first row, so `next()` should return `true` if there is indeed one row. – Marvin Aug 23 '17 at 19:07