Question

In this portion of the code:

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>

<%}
%>

I'm trying to select two values from my table, some of the values can be null, for example I can have a value for my column text_pub and not have a value in my column path_photo and vice versa, so in case I have text_pub and don't have path_photo, I want to display only the text_pub, but the probleme is that the value of text_pub is displayed but also the value of path_photo which is null. PS: I know that treatements like these are better done in servlets, but I really want to make it work in a jsp. Thank you.

No correct solution

OTHER TIPS

Either add a IFNULL check to the sql query:

SELECT IFNULL(text_pub,DEFAULT("")), IFNULL(path_photo,DEFAULT("")) FROM publication,publier,...

or add a check for the return value of resultSet.getString(..)

String text = resultSet.getString("text_pub");
if (text==null) {
    text = "";
}

Edit:

in case I have text_pub and don't have path_photo, I want to display only the text_pub this answer doesn't cover this

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

I know that treatements like these are better done in servlets, but I really want to make it work in a jsp.

You're wrong here. This kind of things are handled in JSP, not in servlet, since logic to present the contents in the view belongs to the View (JSP) and not to the Controller (Servlet). Since you're using scriptlets, you should use an if sentence to validate if you should display the image or not:

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>
<%
}
%>

If you don't want to display anything if resultSet.getString("text_pub") is null:

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>
<%
    }
}
%>

If you happen to write the code in the right way and use a Servlet to handle the data retrieval from database, fill the data in a List<SomeEntity>, add this list into a request attribute (probably with name "photoList") and forward to the desired JSP, then you could use JSTL to control the flow of the image display:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top