
如何设置从数据库中都出来的文字的颜色(jsp)?
id name ifpass
1 aaa y
2 bbb n
3 ccc n
4 ddd y
刚开始没讲明白 我的意思是如果ifpass是Y的话 那么这一行都显示蓝色 如果是N的话 这一行都显示红色 展开
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<html>
<head>
<title></title>
</head>
<body>
<hr>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost:3307/test";
String user="root";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
%>
<table><tr bgcolor=green><th>id</th><th>name</th><th>ifpass</th></tr>
<%
while(rs.next())
{
if(rs.getObject("ifpass").equals("y"))
{
out.println("<tr bgcolor=blue>");
}else{
out.println("<tr bgcolor=red>");
}
%>
<td> <%=rs.getObject("id")%> </td>
<td> <%=rs.getObject("name")%> </td>
<td> <%=rs.getObject("ifpass")%> </td>
</tr>
<%
}
out.println("</table>");
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException e)
{
out.println("驱动程序类异常!<br>");
out.println(e.getMessage());
}
catch(SQLException e)
{
out.println("数据库连接或SQL查询异常!<br>");
out.println(e.getMessage());
}
catch(Exception e)
{
out.println("其他异常!<br>");
out.println(e.getMessage());
}
%>
</body>
</html>
数据库为MYSQL
CREATE TABLE test
(
id int(4) not null,
name char(10),
ifpass char(1)
)character set gbk;
insert into test values(1,"aaa",'y');
insert into test values(2,"bbb",'n');
insert into test values(3,"ccc",'n');
insert into test values(4,"ddd",'y');
String ifpass="y";//自己修改这行接收字段
if("y".equals(ifpass)){
%>
<font color="blue">y就显示蓝色</font>
<%
}else if("n".equals(ifpass)){
%>
<font color="red">n就显示红色</font>
<%
}
%>
String
ifpass="y";//自己修改这行接收字段
if("y".equals(ifpass)){
%>
<font
color="blue">y就显示蓝色</font>
<%
}else
if("n".equals(ifpass)){
%>
<font
color="red">n就显示红色</font>
<%
}
%>