Hi All,

I am trying to write a code which checks whether the username and password stored in the database matches to the string created locally and displays it in console.

The program connects to the database and gets the string from the table space correctly, but the problem is, the if statement is not evaluating to true, the statement inside "if" is not executing.

if(check2 == pass){
System.out.println(check2);         
}

both check2 and pass contains string "apple" . but if i give the same output statement outside "if", its working. im not getting any error message for this.

Please explain. below is the completed code .

package com.tcs.iff.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.*;
import javax.swing.*;

import com.mysql.jdbc.Statement;
import com.tcs.*;
import com.tcs.iff.*;
import com.tcs.iff.login.*;

public class UserService {

    private static int role;
    private static String user="apple";
    private static String pass ="orange";
    private static String check1,check2,check3;

    /**
     * @param args
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */
    public static void main(String[] args) throws ClassNotFoundException {
        // TODO Auto-generated method stub

        Connection connect = null;
        Statement state = null;

        try{
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/iff","root","");
        }catch(SQLException e){
            e.printStackTrace();

        }

        String table = "select role_id, user_name, password from user";

        try {

        state = (Statement) connect.createStatement();  
        ResultSet rs =  state.executeQuery(table);
        while(rs.next()){

            check1 = rs.getString("role_id");
            check2 = rs.getString("user_name");
            check3 = rs.getString("password");

            if(check2 == pass){
                System.out.println(check2);
            }
        }

        connect.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        }


    }   

    }

s1 == s2 checks whether s1 and s2 are references to the same object.

s1.equals(s2) checks whether the value of s1 is equal to the value of s2.

Use .equals

== tests for two expressions representing exactly the same object. Which yours are not!
To test if two string objects contain the same sequence of characters, use
string1.equals(string2)

and change this:

public static void main(String[] args) throws ClassNotFoundException

into a way that you actually handle the exception should it occur. if you keep it like this, you'll never be able to prevent the application from crashing.

having code that might crash is a bad thing, but having that code without decent exception handling, ... that might lead to serious problems.

Thanks stultuske and James ,

Actually i missed to study Exception handling topic in my book, thought its not too important, now that i know its values, i will go thru it. :)

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.