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());
}
}
}