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
[EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to