File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Please help debug this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Please help debug this program" Watch "Please help debug this program" New topic

Please help debug this program

nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
The database table has got 2 fields "username" and "password".The code for accessing the database is given below

If I pass the values that are present in the database table into userpresent.validateUser("z","x") ,then I am getting the ok message .
If I give a value that is not present in the database table then also I get the ok message .
Kindly let me know what the problem could be.
Help would be appreciated.
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
If your program is truely printing the message "ok" then I don't know. But if the userValidatin method is just returning true instead of false you need look no further than your initilization: boolean retval = true;
Follow the logic through, the if-else statement is useless (The validation is done as part of the query). The else part is unreachable.
Also, while your prepared statement will work as is. You should check out the JDBC tutorial to learn how take advantage of their benefits. It would save you having to single quote strings for one.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33113

In addition to what Lu said, you are not checking the username/password if the user is not in the database. Since the returns false, the username and password variables keep the values they had when they were initialized. Since these are the values the user passed in, the if statement will always return true. (You are comparing what the user passed in to what the user passed in.)

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Please help debug this program
It's not a secret anymore!