Reputation:
im trying to compare the two values i get from my database and will return true if BOTH values are null or it doesnt exist in my database. And i already try debugging in netbeans it will jump from rs.getString("post_id").equals(""); to return liked; it will not read the next rs.getString and if statement. here is my code by the way
public boolean likedPost()
{
String thePostID = POSTID();
String likerName = objInfo.name;
boolean liked = true;
try
{
String sql = "select * from Like where post_id=? and people_who_like=?";
pst = conn.prepareStatement(sql);
pst.setString(1,thePostID);
pst.setString(2,likerName);
rs = pst.executeQuery();
boolean comparePostID = rs.getString("post_id").equals("");
boolean compareLiker = rs.getString("people_who_like").equals("");
if(liked == (comparePostID && compareLiker))
liked = true;
else
liked = false;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
return liked;
}
Upvotes: 1
Views: 3293
Reputation: 1951
Compare String using StringUtils class (org.apache.commons.lang.StringUtils). isEmpty()
method of StringUtils class internally checks that given String is null or is Empty or not.
String postId = rs.getString("post_id")
String liker = rs.getString("people_who_like");
if(StringUtils.isEmpty(postId) && StringUtils.isEmpty(postId)) {
liked = true;
} else {
liked = false;
}
EDIT : You can make things simple like below
if (postId!= null && liker!= null && !postId.equals("") && !liker.equals("")) {
liked = true;
} else {
liked = false;
}
Upvotes: 1
Reputation: 2729
String comparePostID = rs.getString("post_id");
String compareLiker = rs.getString("people_who_like");
if(comparePostID && comparePostID && !comparePostID.equals("") && !compareLiker.equals(""))
liked = true;
else
liked = false;
Upvotes: 0