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.MouseEvent event)
      {
            txtEncrypted.setText(txtNew);
            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.odbc.JdbcOdbcDriver");
            System.out.println("Connecting to database (This may take a while...)");
        }
        catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
       
        try {
            con = DriverManager.getConnection(url, "", "");
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(createString);
            System.out.println("Connected");
//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("SQLException: " + ex.getMessage());
            }

      
      }
Nicolas_St_AmourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nicolas_St_AmourAuthor Commented:
Scrap that part of the aforementionned code:

        String txtNew = txtPassword.getText();
      String txtOld = txtPassword.getText();
0
jwilcoxCommented:
I may be wrong, but I belive just using == compares the reference values and not the strings, so you'd need to change user == login to user.equals(login)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tkteeCommented:
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.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Nicolas_St_AmourAuthor Commented:
tktee, could you please elaborate on the exception part?

thanks
0
tkteeCommented:
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?
0
Nicolas_St_AmourAuthor Commented:
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.. :)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.