GeeCON Prague 2014*
The moose likes JDBC and the fly likes Problems with executeQuery-method (PreparedStatement) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problems with executeQuery-method (PreparedStatement)" Watch "Problems with executeQuery-method (PreparedStatement)" New topic
Author

Problems with executeQuery-method (PreparedStatement)

Florian Imbach
Greenhorn

Joined: Aug 07, 2003
Posts: 2
Hi there!
I want to make SQL SELECT, UPDATE and INSERT statements from Java(SDK 1.4.1_02) on MySQL DB (1.4).
For this I use the java.sql.PreparedStatement. Insert and Update works fine over the executeUpdate() method of java.sql.PreparedStatement.
My problem is, that the SELECT doesn't work.
For the SELECT I use the executeQuery(hereIsMySelectStatement) method which should return a java.sql.ResultSet.
Now, here's the point: it doesn't. I have absolutely no idea why, but the method returns null. Can you help please?
Thanks in advance!
Here's my code:
---------------------------------------------------
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.RowSet;
public class DatabaseRetriever
{
Connection myConnection = null;
public ResultSet retrieve(String aSQLQueryString) throws SQLException
{
ResultSet result = null;
PreparedStatement prepStatement = null;

myConnection = DatabaseConnection.getConnection();

if (myConnection != null)
{
prepStatement = myConnection.prepareStatement(aSQLQueryString);
result = prepStatement.executeQuery(aSQLQueryString);

result.next();
}
return result;
}
} // end of class
---------------------------------------------------
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

It should be:

The method without a parameter uses a prepared statement. The method with a parameter uses a regular statement. I don't think this is causing the error, but it's still good to know.
If this doesn't solve the problem, please post the error message you are getting and the SQL select statement you are passing.


[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
Florian Imbach
Greenhorn

Joined: Aug 07, 2003
Posts: 2
Originally posted by Jeanne Boyarsky:
It should be:

The method without a parameter uses a prepared statement. The method with a parameter uses a regular statement. I don't think this is causing the error, but it's still good to know.

I don't believe it! :-)
I just deletetd the String in the executeQuery method and now it works like it should!
Thanks a lot! You saved the day! ;-)
With kind regards, Florian Imbach
 
GeeCON Prague 2014
 
subject: Problems with executeQuery-method (PreparedStatement)