You are setting username and password as parameters to de PreparedStatement, not to the Connection.
There are no results when the query is executed with the first parameter equal to username!!



Dirk Verbeeck wrote:


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


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







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



-------------------------------------------------------------------------------------------------------------------
Este correo electr�nico y, en su caso, cualquier fichero anexo al mismo, contiene 
informaci�n de car�cter confidencial exclusivamente dirigida a su destinatario o 
destinatarios. Queda prohibida su divulgaci�n, copia o distribuci�n a terceros sin la 
previa autorizaci�n escrita de Indra. En el caso de haber recibido este correo 
electr�nico por error, se ruega notificar inmediatamente esta circunstancia mediante 
reenv�o a la direcci�n electr�nica del remitente.

The information in this e-mail and in any attachments is confidential and solely for the attention and use of the named addressee(s). You are hereby notified that any dissemination, distribution or copy of this communication is prohibited without the prior written consent of Indra. If you have received this communication in error, please, notify the sender by reply e-mail

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



Reply via email to