Hi

I'm doing a project which requires a registration/login page. I have created both registration and login html pages and have done the validation servlet for the login page. I've also attempted the validation servlet for the registration page, but this is where I am struggling because I can't get the servlet to compare the username entered in the html form to usernames in the database to prevent duplicate usernames. This is the sql statement I have used...

...
try{
String uniqueSQL = "select * from members where username = ' "+username+" ' ";

Statement stmt = conn.createStatement();
ResultSet regvalidate = stmt.executeQuery(uniqueSQL);
...
if (ResultSet == username){
}
...

Does anyone have any ideas?


Thank you

Recommended Answers

All 2 Replies

You need to check the information in the results set. First you need to see if the result set contains any information (this is not the check for null). Next (providing there is information) you need to compare the username and the password of from the html to the record in the database. If one matches then they can log in otherwise invalid username/password message.

Hopefully that gives you the right idea.

Cheers

First of all you have some spaces at the query that will not get you right results: "select * from members where username = ' "+username+" ' " Should be: "select * from members where username = '"+username+"' " Second: the "ResultSet" is the name of the class and the "username" is a String variable. Shouldn't you focusing more on learning simpler things, because form what I see from your code, what you are trying to achieve is way above your skills. You don't even know how to compare 2 variables or use class instances and you are trying to write a login page?

Anyway, first check the API for the ResultSet. There you will find methods that will help you extract the data from the ResultSet object. Better yet search this forum for examples on how to read data from the database.

As for your query it is better instead of this "*" to put the actual columns: "select username, password from members where username = '"+username+"' " Then take the username, password from the ResultSet and compare it with what you get from the GUI

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.