2017-05-26 76 views
0

我只是簡單地試圖從表tblUser中提取數據,並在下面的代碼中使用足夠的格式顯示isActive,以顯示紅色&如果顯示「否」且綠色和粗體顯示爲正常重量是。它所顯示的所有結果都是綠色和大膽的。JSP/MySQL - 不採取格式

<%@ page import="com.mysql.*" %> 
<%@ page import="java.sql.*" %> 

<html> 
<body> 
<div id="content"> 


    <p>Displaying table contents: </p> 

    <table border="0" cellpadding="10"> 
     <thead> 
      <tr> 
       <th>User ID</th> 
       <th>First Name:</th> 
       <th>Last Name:</th> 
       <th>Notes</th> 
       <th><b>Is Active?</b></th> 
      </tr> 
     </thead> 
     <tbody> 


      <% 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection conn = null; 
       conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/raa", "root", "root"); 
       Statement stmt = null; 
       stmt = conn.createStatement(); 
       String query = "SELECT userID, Group_id, role, fname, lname, email, password, phone, notes, case when isActive = 1 then 'Yes' else 'no' end isActive FROM raa.tblUser"; 
       ResultSet rs = null; 
       rs = stmt.executeQuery(query); 
       while(rs.next()){ 
      %> 
      <tr> 
       <% 
        int userid = rs.getInt("userid"); 
        String fname = rs.getString("fname"); 
        String lname = rs.getString("lname"); 
        String notes = rs.getString("notes"); 
        String isActive = rs.getString("isActive"); 
       %> 
       <td><%=userid %></td> 
       <td><%=fname %></td> 
       <td><%=lname %></td> 
       <td><%=notes %></td> 
       <% 
        String activeClass = ""; 
        String activeBold = "";     
        if(isActive == "0"){ 
         activeClass = "red"; 
         activeBold = "normal"; 
        } 
        else{ 
         activeClass = "green"; 
         activeBold = "bold"; 
        } 
       %> 
       <td style="color: <%=activeClass%>; font-weight: <%=activeBold %>;"><%=isActive%></td> 
      </tr>    

      <%  
       } 
      %> 

     </tbody> 
    </table> 
</div> 
</body> 
</html> 

而這裏的結果頁面: Results page

回答

1

您必須使用等於對象上比較... isActive.equals( 「0」)「0」 .equals(isActive)

   <% 
        String activeClass = ""; 
        String activeBold = "";     
        if("0".equals(isActive)){ 
         activeClass = "red"; 
         activeBold = "normal"; 
        } 
        else{ 
         activeClass = "green"; 
         activeBold = "bold"; 
        } 
       %>