aspose file tools*
The moose likes JDBC and the fly likes Why can I INSERT but not SELECT? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Why can I INSERT but not SELECT?" Watch "Why can I INSERT but not SELECT?" New topic
Author

Why can I INSERT but not SELECT?

Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
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).

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158

Angus,
Welcome to JavaRanch!

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
Jeanne Boyarsky wrote:Angus,
Welcome to JavaRanch!

Thank you!

When you say the result set is null, do you mean empty? A result set should never be null.

I mean that the ResultSet object itself is null (according to jdb). But no exception is thrown by st.executeQuery ().

So even if I remove the first rs.next () statement, I still get a NullPointerException. (but don't you have to call next() to access the first record?)



Avyaya Avi
Greenhorn

Joined: Feb 04, 2009
Posts: 11
You NEED to call rs.next() in order to enter the result set values anyway.

What I suggest may be lame, but consider once and do let me know.

Move



to the top and execute.

Just a close call.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
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.

Move



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!
Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Is DatabaseConnection one of your classes? Any chance we can see the source?
I just ask since last time I saw this, it was related to a custom-written JDBC framework not behaving correctly.
Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
I pretty much just lifted it from Horstmann & Cornell's Core Java Vol 2.



and this is database.properties

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Angus McIntosh wrote:

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.
Angus McIntosh
Greenhorn

Joined: Jan 28, 2009
Posts: 12
I must not understand the output of jdb, because what Bauke Sholtz suggested fixed my problem.

Thanks a lot!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can I INSERT but not SELECT?