简体   繁体   中英

How do I check for the null value in Java?

How to look for the value whether it is null or not in JSP. Eg in below I want to check the description column in pipe table for null values.

String Query1 = "SELECT description FROM pipe where pipe_id='" + id+"' and version_id=2";
SQLResult = SQLStatement.executeQuery(Query1);
SQLResult.first();
description = SQLResult.getString("description");
if(description=="")
{
    json_string=json_string+"&desc="+description;
    out.println("not null");
} else
{
    json_string=json_string;
    out.println("null");
}

Well: if (description != null && !description.isEmpty()) ... however are you writing this code within a JSP ? That seems wrong. You should probably do this stuff within a servlet and pass the description as an request attribute to a jsp.

或者,您可以使用StringUtils.isNotEmpty(description)

如果您确实在使用jsp,则应将描述存储在request属性中,然后使用<c:if test="${empty description}">

I use this personal method:

public static boolean isNullOrEmpty(Object obj) {
    if (obj == null || obj.toString().length() < 1 || obj.toString().equals(""))
        return true;
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM