In my webapp (an educational games database), I can add games to the database no problem, but I can't access the set of games once they're there.
Using jdb with the following class, I found that st.executeQuery (GAMES_LIST_QUERY) is returning null. No exceptions are thrown, and the class seems to be connecting properly with the database.
When I run the exact same query in mysql on the cli, though, I get the results I want (I cut and pasted, just to be sure).
When you say the result set is null, do you mean empty? A result set should never be null.
This code doesn't do what you think it does. The first time you call rs.next() [in the if stmt] the result set pointer advances to the second record. So by the time you get to the look, it is looking past it. Which means you are losing the first element. And if you have only one element returned, it will look like an empty list.
As Jeanne has already said, the ResultSet should not be null. Which Driver are you using?
Also, why are you closing your results set in the same loop you use to iterate through it? That will cause a problem; the result set cannot move the cursor to the next row if it is closed, and without doing that your loop condition cannot be tested.
Avyaya Avi wrote:You NEED to call rs.next() in order to enter the result set values anyway.
That' what I thought!
What I suggest may be lame, but consider once and do let me know.
to the top and execute.
It doesn't make a difference, but thanks!
It seems I bumped 14 seconds after you posted! And here I though I was being neglected!
Joined: Jan 28, 2009
Paul Sturrock wrote:As Jeanne has already said, the ResultSet should not be null. Which Driver are you using?
I'm using mysql-connector-java-5.1.6, the one in the Intrepid repositories. I'm able to INSERT in another part of my app with it. But when I run the above code, I get a null object, and my test code crashes with a NullPointerException.
Also, why are you closing your results set in the same loop you use to iterate through it?
I'm not, I'm closing it in the _if_ block, although I suppose it would make more sense to close it outside the if block.
Leave the fetchSize away. This makes no sense. It does not represent the amount of the obtained rows which you seem to think, but it roughly represents the amount of rows to cache at once.
At end you should use an ArrayList to get hold of all rows, not an array. The amount of rows can't be determined beforehand without nasty workarounds. Just add new row objects to the ArrayList using List#add() method.
Joined: Jan 28, 2009
I must not understand the output of jdb, because what Bauke Sholtz suggested fixed my problem.