Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with executeQuery-method (PreparedStatement)

 
Florian Imbach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34195
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Florian Imbach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic