Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PreparedStatement & MySql not working ! Why ?!!?

843854Apr 30 2004 — edited May 25 2004
Hello everyone. One question for you : I try to have a SELECT on a table to get a column value. I've used for that a preparedstatement but id didn't worked, I mean it returned nothing. Then I've used a simple statement which worked correctly. My question is why prepared doesn't work while simple statement do ?! Other querys work fine, but this one doesn't. And I simply don't get it... I need this preparedstatement way because I use this query very often.
I use MySQL Connector/J latest version, Java 1.4. Here's the code for this situation:

This doesn't work:

String sql = "SELECT IDUSER FROM SUBSCRIBERS WHERE MSISDN = ? AND SUBSCRIBER_TYPE = 0 AND RIGHTS <> 1 LIMIT 1";
PreparedStatement pstm = null;
ResultSet rs = null;

pstm = serviceConnection.prepareStatement(sql);
pstm.setString(1, MSISDN);
rs = pstm.executeQuery();

//gets the iduser from the query
if (rs.next())
iduser = rs.getInt("IDUSER");
else
iduser = 0;

This works:

String sql = "SELECT IDUSER FROM SUBSCRIBERS WHERE MSISDN = "+MSISDN+" AND SUBSCRIBER_TYPE = 0 AND RIGHTS <> 1 LIMIT 1";
Statement stm = null;
ResultSet rs = null;

stm = serviceConnection.createStatement();
rs = stm.executeQuery(sql);

//gets the iduser from the query
if (rs.next())
iduser = rs.getInt("IDUSER");
else
iduser = 0;

Thank's in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 22 2004
Added on Apr 30 2004
8 comments
1,102 views