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.