Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in returning the username

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
In the below code I want to return the username it uname equals the name in db and password equals the pwd in db. I don't know how to give return statement properly. Please help me.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error happens because the "result" variable is declared in the while loop. You could have something looking like this instead :

Remove the "else" block, because you want to loop through all record before setting the result. You also need to close all resources in a finally block instead of in the try/catch block.

However,
1. It would be easier to make an SQL statement like "select * from details where uname=? and pwd=?" (or whatever the field names are), and use statement.setString(1, name) and statement.setString(2, pwd).
2. The name of your method is confusing. It looks like it just connects to a DB. It looks like name and pwd are values used for the JDBC connection. You should separate the db connection stuff, and the user check stuff in separate methods.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result variable's scope is limited to the while loop. You need to declare it out side the while loop. Actually the second return should give an compile time error.

For the First return is not valid as there are statements to be executed after return. So those statements become unreachable and the compiler will return error.

Best way is to use a PreparedStatment and the following SQL query:


And then you can set the values for username and password.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic