I have used if loop to compare the values present in table and the value entered from the user, but in the code its not entering to if loop rather it is entering to else loop, i need the suggestion for the problem.
Here is the code :
public void idExists(String SkillID) {
try{
Connection conn = dbconnect();
开发者_StackOverflow中文版conn.setReadOnly(false);
Statement st = conn.createStatement();
String skilid= "Select [Skill ID]from [Skill Master$]";
ResultSet rs = st.executeQuery(skilid);
while(rs.next()){
String Skill = rs.getString("Skill ID");
System.out.println(SkillID);
}
if (SkillID==Skill) {
System.out.println("the skill id exist");
}
else {
System.out.println("the skill id doesnt exist");
}
endConnect();
}
catch(SQLException e) {
System.err.println("Got an exception in idexist");
System.err.println(e.getMessage());
}
}
}
You're comparing string value by reference.
Since your variable refer to two different String
instances with the same value, the condition is false.
You should compare strings using the equals
method.
Also, you probably want to put the if
inside the loop.
Finally, you should replace all of that code with a (parameterized!) WHERE
clause.
精彩评论