SQL record set manipulation for login

Posted on 2000-02-09
Medium Priority
Last Modified: 2010-04-01
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)

        //SQL statements below
        String url = "jdbc:odbc:User";
        Connection con;
        String createString;
        createString = "select Username, Password from tblpwd";
        Statement stmt;

        try {
            System.out.println("Connecting to database (This may take a while...)");
        catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
        try {
            con = DriverManager.getConnection(url, "", "");
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(createString);
//check password and give access...
            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");
                txtSQL.setText("Access Denied");
            System.out.println(user + "/" + login);
            catch(SQLException ex) {
               System.err.println("SQLException: " + ex.getMessage());

Question by:Nicolas_St_Amour
  • 3
  • 2

Author Comment

ID: 2505060
Scrap that part of the aforementionned code:

        String txtNew = txtPassword.getText();
      String txtOld = txtPassword.getText();

Accepted Solution

jwilcox earned 60 total points
ID: 2505574
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)

Expert Comment

ID: 2506197
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.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Author Comment

ID: 2507600
tktee, could you please elaborate on the exception part?


Expert Comment

ID: 2510024
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?

Author Comment

ID: 2511578
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.. :)


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question