• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Problem in returning the username

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic