File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes number of rows in ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "number of rows in ResultSet" Watch "number of rows in ResultSet" New topic
Author

number of rows in ResultSet

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm not sure if this a jdbc problem, but here i go:
i'v only one entry in table login;
i'm checking number of returned rows form a query with:

if i hard code my query, i get numberOfRows = 1, but if i build it dinamically i allways get zero, and dont know why!
take a look:

i also confirmed that both requests return the correct input

what might i've been doing wrong?

TiA
[ November 22, 2006: Message edited by: miguel lisboa ]

java amateur
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
"SELECT * FROM login where nome = 'username' and palavrapasse = 'password'"; // returns 0


This does not build the query dynamically from the Strings username and password. It just hard codes the nome to be "username" and palavrapasse to be "password".

You need to build the string using the variables. There are many ways to accomplish this.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
You should really use PreparedStatement to avoid sql injection. Search this forum or google for more info on how and why.

The JavaRanch security faq links to two articles on SQL injection. One mentions prepared statements but not java specifically.

Or see the comment in PreparedStatement about bad things happening.
[ November 22, 2006: Message edited by: Carol Enderlin ]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
case solved
in case someone has the same prob, here's the solution:
"SELECT * FROM login where nome = '"+username+"' and palavrapasse = '" + password + "'";
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
@Carol
thanks a lot for your sugestion!

and also you gave me the oportunity to have my first prepared statement
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of rows in ResultSet
 
Similar Threads
java .lang .Number Format Error
Login Verification
java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).
Not able to forward servlet to
Help with this code!