Validating a user based on result.next()

I have a user id in my database and I want to validate that user id from another program. So is it good enough to validate that user id based on my

resultSet.next() with an appropriate query

for example

publicboolean validateUser(String userLogon){

-

PreaparedStatement ps = prepareStatement("SELECT userId from myTable where UserLogon = '" + userLogon +"'");

ResultSet rs = ps.executeQuery();

return rs.next();//return true if userId is valid and false otherwise

}

This kind of a validation is robust enough, any comments?

i am expecting only one user id for a usrLogon.

[877 byte] By [sunish_josea] at [2007-11-27 7:09:38]
# 1
returning a result set?you should put them (connection, statement, result set or any resource) in a try...finally block. put the release resource code in finally block to ensure the resource be released.
j_shadinataa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
PreaparedStatement ps = prepareStatement("SELECT userId from myTable where UserLogon = '" + userLogon +"'");what if UserLogon =' or 1=1;
j_shadinataa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I agree i have to catch all exceptions i know that, it is only a code snippet. Here I will get a valid userId if userLogon i am passing is correct, if not i won't get a user id. The question in if userLogon is invalid rs.next() will return false or not? and this typical validation got any flaws?

sunish_josea at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

He didn't mean that :)

Read something more about PreparedStatements.

Here is a JDBC tutorial http://java.sun.com/docs/books/tutorial/jdbc/

PreparedStatements are covered here: http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

Also read some more about SQL injections.

BalusCa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

1. your query are welcoming sql injection. when userlogon = "some' or or 'thing' = 'thing" then your complete query become:

select from where somecolumn = 'some' or 'thing' = 'thing'

... oops... it's will return true. i can login even i don't have valid user name.

2. when using rs.next() to determinate validity of user, your application may become cAsE iNsEnSiTiVe if your database server is not case sensitive.

j_shadinataa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Thanks very much guys for ur comments.
sunish_josea at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

But how to prevent SQL injection?

If i use

PreaparedStatement ps = prepareStatement("SELECT userId from myTable where UserLogon = ?'");

ps.setString(1, userLogon);

will avoid SQL injection? Pls let me know.

Message was edited by:

sunish_jose

sunish_josea at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
yup.it will prevent sql injection. prepared statement also help us with other data type that can hard in formatting like datetime & blob.
j_shadinataa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
Thanks very much j_shadinata
sunish_josea at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
welcome
j_shadinataa at 2007-7-12 19:01:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...