2014-03-04 27 views
0

在這部分代碼:顯示沒有空值在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_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> 

<%} 
%> 

我想從我的表中選擇兩個值,有些值可以爲空,例如我可以有一個值爲我的列text_pub,並沒有在我的列path_photo價值,反之亦然,所以如果我有text_pub和沒有path_photo,我想只顯示text_pub,但問題是,text_pub的值是顯示,但也是null的path_photo的值。 PS:我知道像servlet這樣的處理在servlet中更好,但我真的想讓它在jsp中工作。謝謝。

回答

0

要麼添加IFNULL檢查SQL查詢:

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

或添加檢查resultSet.getString的返回值(..)

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

編輯:

萬一我有text_pub並且沒有path_photo,我想顯示 只有text_pub這個答案不包括此

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

我想你的解決方案R. Oosterholt,但現在沒有任何dispaly。 – higheagle

+0

哪一個?查詢改進或getString返回值檢查? –

+0

*如果我有text_pub並且沒有path_photo,我只想顯示text_pub *這個答案不包括這個。 –

0

我知道,像這樣的treatements更好地在servlet中完成的,但我真的希望把它在一個jsp工作。

你錯了。這類事情在JSP中處理,而不是在servlet中處理,因爲在視圖中呈現內容的邏輯屬於View(JSP)而不屬於Controller(Servlet)。由於您使用的小腳本,您應該使用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> 
<% 
} 
%> 

如果不希望顯示任何東西,如果resultSet.getString("text_pub")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> 
<% 
    } 
} 
%> 

如果你碰巧寫在正確的方式代碼,並使用一個Servlet來處理來自數據庫的數據檢索,在List<SomeEntity>填充數據,添加此列表到一個請求屬性(可能與名稱"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> 
+0

我試過你的解決方案luiggi和R. Oosterholt'的解決方案,文本和照片總是顯示,並且當它爲空並顯示空白空間時。 – higheagle

+0

@maroua如果'text_pub'爲null,那麼它不應該顯示任何內容? –

+0

是的,但問題是更多的使用path_photo,因爲當照片路徑爲空時,我得到一個有點十字的空間,就像有一張照片但路徑錯誤。它就像這個操作「with path_photo = null – higheagle