File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problem with executeQuery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem with executeQuery" Watch "Problem with executeQuery" New topic

Problem with executeQuery

Tokai Moshai

Joined: May 24, 2006
Posts: 16
I have a simple code where I'm having problem with executeQuery:

Strange thing is, when I copy the exact query from here to MySQL query browser, a result set is returned. I get database connection established message (code not showing here) and I do not get any exception. But I get r = 0 does not matter what.

Another thing is, just for checking when I used executeUpdate with an INSERT command it does insert the data into the database. What could be the reason INSERT is working but SELECT is not?
Dileep Kamath

Joined: Jul 15, 2006
Posts: 19
Hi Tokai,
I've also tried using the same api to get the number of rows returned, but it did not work. The driver might be using optimising techniques to get the results as the result set might be huge and the query might take a lot of time to execute. it be better to have a count of rows returned as well and check that column to get the fetch size. i'm sure if you iterate over the result set you'd see that there are results returned for the query.
Tokai Moshai

Joined: May 24, 2006
Posts: 16
No the result set is actually empty. It in the example I was doing, it returns 2 results when executed the query from query browser, and returns zero results when executed from the code. I would say that was a problem with connection, but then how come INSERT works?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

First of all that code will throw a NullPointerException because you don't have any code that executes a query. But let's suppose your actual code has that code not commented out. The next line of code doesn't tell you the number of records that would be in the ResultSet if you read them all, it tells you the fetch size of the ResultSet. That is a number that you can set to tell the server how many records to return at a time, it has nothing to do with the number of actual records.

So if that is what you base "returns zero results" on, don't do that. Read through the ResultSet and see what you get.
I agree. Here's the link:
subject: Problem with executeQuery
It's not a secret anymore!