Link to home
Start Free TrialLog in
Avatar of Nicolas_St_Amour
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.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());
            }

      
      }
Avatar of Nicolas_St_Amour
Nicolas_St_Amour

ASKER

Scrap that part of the aforementionned code:

        String txtNew = txtPassword.getText();
      String txtOld = txtPassword.getText();
ASKER CERTIFIED SOLUTION
Avatar of jwilcox
jwilcox

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
tktee, could you please elaborate on the exception part?

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?
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.. :)