aspose file tools*
The moose likes Beginning Java and the fly likes Problem in returning the username Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in returning the username" Watch "Problem in returning the username" New topic
Author

Problem in returning the username

sahana mithra
Ranch Hand

Joined: Oct 26, 2010
Posts: 72
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

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.

Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in returning the username
 
Similar Threads
Databse Problem
Everytime getting an empty ResultSet
Java to Display Data from Database
Need help in Connecting my MySQL Database to JTable in Eclipse.
problem in connecting to oracle through jdbc