This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30124
    
150

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems with executeQuery-method (PreparedStatement)
 
Similar Threads
Closing a db connection
What is -Xlint in compiling java program?why this is done?
Writing CLOB
(jdbc+oracle) urgent please!!
Another "com.mysql.jdbc.Driver" Connection problem