Nicolas_St_Amour
asked on
SQL record set manipulation for login
Hi can anyone help me with this code?
The program connects to the DB, it even shows the record set value I want in a system.out line, but when I try to compare a string (login for instance) and verify if it's equal to a string in the record set (under Username field...)
Anyways, here's my code:
(it's just the btnLogin_clicked part... which should be enough)
////////////////////////// ///
void btnLogin_MouseClicked(java .awt.event .MouseEven t event)
{
txtEncrypted.setText(txtNe w);
txtDecrypted.setText("");
//SQL statements below
String url = "jdbc:odbc:User";
Connection con;
String createString;
createString = "select Username, Password from tblpwd";
Statement stmt;
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
System.out.println("Connec ting to database (This may take a while...)");
}
catch(java.lang.ClassNotFo undExcepti on e) {
System.err.print("ClassNot FoundExcep tion: ");
System.err.println(e.getMe ssage());
}
try {
con = DriverManager.getConnectio n(url, "", "");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(createSt ring);
System.out.println("Connec ted");
//check password and give access...
while(rs.next()){
String user = rs.getString("Username");
String pass = rs.getString("Password");
String login = txtLogin.getText();
String password = txtPassword.getText();
if (user == login){
txtSQL.setText("Access Granted");
}
else{
txtSQL.setText("Access Denied");
}
System.out.println(user + "/" + login);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
System.err.println("SQLExc eption: " + ex.getMessage());
}
}
The program connects to the DB, it even shows the record set value I want in a system.out line, but when I try to compare a string (login for instance) and verify if it's equal to a string in the record set (under Username field...)
Anyways, here's my code:
(it's just the btnLogin_clicked part... which should be enough)
//////////////////////////
void btnLogin_MouseClicked(java
{
txtEncrypted.setText(txtNe
txtDecrypted.setText("");
//SQL statements below
String url = "jdbc:odbc:User";
Connection con;
String createString;
createString = "select Username, Password from tblpwd";
Statement stmt;
try {
Class.forName("sun.jdbc.od
System.out.println("Connec
}
catch(java.lang.ClassNotFo
System.err.print("ClassNot
System.err.println(e.getMe
}
try {
con = DriverManager.getConnectio
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(createSt
System.out.println("Connec
//check password and give access...
while(rs.next()){
String user = rs.getString("Username");
String pass = rs.getString("Password");
String login = txtLogin.getText();
String password = txtPassword.getText();
if (user == login){
txtSQL.setText("Access Granted");
}
else{
txtSQL.setText("Access Denied");
}
System.out.println(user + "/" + login);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
System.err.println("SQLExc
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
jwilcox is correct. To compare the value of string, use 'equals' rather than '='.
One thing though. You might want to exit the loop once a match if found. You can do this by throwing your own exception within the loop and catch it since your loop in within a try-catch block.
One thing though. You might want to exit the loop once a match if found. You can do this by throwing your own exception within the loop and catch it since your loop in within a try-catch block.
ASKER
tktee, could you please elaborate on the exception part?
thanks
thanks
Imagine this, there are 100 user ids in the ResultSet and the record matching the ID is record number 3. Having found the record, there is no need to process the remaining records; 4-100.
Therefore, it would be wiser to exit the while(rs.next()) loop once a record is found. This can be done by throwing an exception (your own exception) to exit from the loop.
You can do this as follows:
1. Insert the following line after the statement txtSQL.setText("Access Granted");
throw new RecordMatchException();
2. Catch the exception. You may place the the following catch block before your SQLException catch.
catch (RecordMatchException e) {
// put whatever relevant code you wish, here
}
3. Create the RecordMatchException class.
public class RecordMatchException extends Exception {
public RecordMatchException() {}
}
Note that you may named the exception name (RecordMatchException) to whichever name you want. Just remember to create the corresponding class. :-)
You may email me at tktee@yahoo.com OR just post a comment here, should you need more information.
Does the above explantion of any help to you?
Therefore, it would be wiser to exit the while(rs.next()) loop once a record is found. This can be done by throwing an exception (your own exception) to exit from the loop.
You can do this as follows:
1. Insert the following line after the statement txtSQL.setText("Access Granted");
throw new RecordMatchException();
2. Catch the exception. You may place the the following catch block before your SQLException catch.
catch (RecordMatchException e) {
// put whatever relevant code you wish, here
}
3. Create the RecordMatchException class.
public class RecordMatchException extends Exception {
public RecordMatchException() {}
}
Note that you may named the exception name (RecordMatchException) to whichever name you want. Just remember to create the corresponding class. :-)
You may email me at tktee@yahoo.com OR just post a comment here, should you need more information.
Does the above explantion of any help to you?
ASKER
It's ok, I managed to do it all in a while loop...
I created an integer i that starts with the value 0 and put in the code as follows:
while (i<1 && rs.next){
//record set stuff...
}
and it seems to be working fine now... but thanks anyways.. :)
I created an integer i that starts with the value 0 and put in the code as follows:
while (i<1 && rs.next){
//record set stuff...
}
and it seems to be working fine now... but thanks anyways.. :)
ASKER
String txtNew = txtPassword.getText();
String txtOld = txtPassword.getText();