Java 在jsp中的查询中显示非空值

Java 在jsp中的查询中显示非空值,java,mysql,jsp,Java,Mysql,Jsp,在本部分代码中: try { connection = DriverManager.getConnection( connectionUrl + dbName, userId, password); statement = connection.createStatement(); String sql = "SELECT text_pub, path_photo FROM publication,publier, consomateur WHERE publication.id_public

在本部分代码中:

try {
connection = DriverManager.getConnection(
connectionUrl + dbName, userId, password);
statement = connection.createStatement();
String sql = "SELECT text_pub, path_photo  FROM publication,publier, consomateur WHERE publication.id_publication=publier.publication_id_publication AND publier.users_id_user=consomateur.code_bar AND consomateur.code_bar = "+idUser+" AND 'path_photo' IS NOT NULL AND 'text_pub' IS NOT NULL";

resultSet = statement.executeQuery(sql);



while (resultSet.next()) {
%>

    <tr bgcolor="#8FBC8F">

<td><%=resultSet.getString("text_pub")%></td>
<img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130"></td>

<%}
%>
试试看{
connection=DriverManager.getConnection(
connectionUrl+dbName、userId、密码);
statement=connection.createStatement();
String sql=“从publication、publier、consomateur中选择text\u pub、path\u photo,其中publication.id\u publication=publier.publication\u id\u publication和publier.users\u id\u user=consomateur.code\u bar和consomateur.code\u bar=“+idUser+”和“path\u photo”不为NULL,“text\u pub”不为NULL”;
resultSet=statement.executeQuery(sql);
while(resultSet.next()){
%>
“width=“150”height=“130”>
我试图从我的表中选择两个值,其中一些值可以为null,例如,我可以在我的列text_pub中有一个值,而在我的列path_photo中没有值,反之亦然,因此,如果我有text_pub而没有path_photo,我只想显示text_pub,但问题是text_pub的值显示为but还包括路径_photo的值,该值为空。
PS:我知道这样的处理最好在servlet中完成,但我真的想让它在jsp中工作。谢谢。

或者在sql查询中添加一个IFNULL检查:

SELECT IFNULL(text_pub,DEFAULT("")), IFNULL(path_photo,DEFAULT("")) FROM publication,publier,...
或者添加对resultSet.getString(..)返回值的检查

编辑:

如果我有文本发布,但没有路径照片,我想显示 只有这个答案的文本不包括这个


“width=“150”height=“130”>
我知道这样的处理最好在servlet中完成,但我真的想让它在jsp中工作

这里你错了。这类事情是在JSP中处理的,而不是在servlet中处理的,因为在视图中显示内容的逻辑属于视图(JSP),而不是控制器(servlet)。因为你使用的是Scriptlet,你应该使用
if
语句来验证你是否应该显示图像:

while (resultSet.next()) {
%>
    <tr bgcolor="#8FBC8F">
        <td><%=resultSet.getString("text_pub")%></td>
        <td>
            <%
            if (resultSet.getString("path_photo") != null) {
            %>
                <img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130">
            <%
            }
            %>
        </td>
    </tr>
<%
}
%>

如果您碰巧以正确的方式编写了代码,并使用Servlet处理从数据库检索的数据,将数据填入
列表
,将此列表添加到请求属性(可能具有名称
“photoList”
)并转发到所需的JSP,那么您可以使用JSTL控制图像显示的流程:

<c:forEach items="${photoList}" var="photo">
    <tr bgcolor="#8FBC8F">
        <td>${photo.textPub}</td>
        <td>
            <c:if test="${not empty photo.pathPhoto}">
                <img src="images/${photo.pathPhoto}" width="150" height="130">
            </c:if>
        </td>
    </tr>
</c:forEach>

${photo.textPub}

我尝试了你的解决方案R.Oosterholt,但现在它没有显示任何内容。哪一个?查询改进还是getString返回值检查?如果我有文本发布,没有路径照片,我只想显示文本发布。这个答案不包括这一点。我尝试了你的解决方案luiggi和R.Oosterholt的解决方案,文本和照片总是如果
text\u pub
为空,那么它就不应该显示任何内容?是的,但是路径照片的问题更大,因为当照片路径为空时,我会得到一个带有一个小十字的空间,就像有一张照片,但路径是错误的。就像这个操作一样“带着一张照片=null@maroua这是不可能的。这意味着您的代码尚未更新。请确保重新编译源代码,重新部署web应用程序,并从浏览器中清除缓存。
while (resultSet.next()) {
%>
    <tr bgcolor="#8FBC8F">
        <td><%=resultSet.getString("text_pub")%></td>
        <td>
            <%
            if (resultSet.getString("path_photo") != null) {
            %>
                <img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130">
            <%
            }
            %>
        </td>
    </tr>
<%
}
%>
while (resultSet.next()) {
    if (resultSet.getString("text_pub") != null) {
%>
    <tr bgcolor="#8FBC8F">
        <td><%=resultSet.getString("text_pub")%></td>
        <td>
            <%
            if (resultSet.getString("path_photo") != null) {
            %>
                <img src="images/<%=resultSet.getString("path_photo")%>" width="150" height="130">
            <%
            }
            %>
        </td>
    </tr>
<%
    }
}
%>
<c:forEach items="${photoList}" var="photo">
    <tr bgcolor="#8FBC8F">
        <td>${photo.textPub}</td>
        <td>
            <c:if test="${not empty photo.pathPhoto}">
                <img src="images/${photo.pathPhoto}" width="150" height="130">
            </c:if>
        </td>
    </tr>
</c:forEach>