commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dirk Verbeeck <dirk.verbe...@pandora.be>
Subject Re: can't set parameters in prepared statements with DBCP and jakarta.
Date Tue, 25 Nov 2003 21:27:23 GMT
Why are you using the two stmt.setString in your second code example?
Using the normal stmt.setInt should work just fine.

-- Dirk

Sergio Gonzalez wrote:

> Hello.
> 
> I'm new to the list, so please be patient with me. :-)
> 
> 
> I looked deeply on the net about a little problem I've trying to change 
> my servlets from normal sql connections (mysql thru jdbc driver), to the 
> pooled version using DBCP in a tomcat powered web server.
> 
> when doing a normal connection I do something like:
> 
> url = "jdbc:mysql://localhost/testdb";
> Class.forName ("com.mysql.jdbc.Driver");
> Connection conn = DriverManager.getConnection (url, "user", "password");
> String query = "SELECT FIELD1 FROM TABLE1 WHERE FIELD2 = ?";
> preparedStatement stmt = conn.prepareStatement(query);
> stmt.setInt(1,1234567890);
> ResultSet result = stmt.executeQuery();
> 
> and everything goes well. I can retrieve data from the resultset, etc; 
> but when using this:
> 
> Context ctx = null;
> Connection conn = null;
> PreparedStatement stmt = null;
> DataSource ds = null;
> String query = "SELECT FIELD1 FROM TABLE WHERE FIELD2 = ?";
> 
> try{
>         ctx = new InitialContext();
>         if(ctx == null )
>                 hrow new Exception("No Context");
>         ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
> 
>         if (ds != null){
>                 conn = ds.getConnection();
>                 stmt = conn.prepareStatement(query);
>                 stmt.setString(1,username);
>                 stmt.setString(2,password);
>                 ResultSet result = stmt.executeQuery();
>                 if(respuesta.next()){
>                         ...
>                 }else{
>                         System.err.println("no rows retrieved");
>                 }
>         }
> }catch(SQLException e)
> {
> }
> 
> The query is executed, but I just can't get any rows. Then I looked into 
> the mysqld.log and I found the query was made but without any parameters.
> 
> The resultset give me rows when I do it directly, I mean:
> 
> query = "SELECT FIELD1 FROM TABLE WHERE FIELD2 = 
> "+String.valueOf(1234567890);
> 
> What am I doing wrong?
> 
> 
> 
> Thanks a lot in advance for the help.
> 
> 
> 
> 
> 
> Sergio Gonzalez
> sagonzal@sky.net.co
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message