如果数据库中没有条目,则避免JSP表文本框中的空值

时间:2012-06-11 13:38:47

标签: mysql jsp

String sql="SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,a.DEPARTMENT_TEXT,a.FLOOR,b.BELONGING_TYPE,b.BELONGING_TEXT ,a.INTIME, c.EXIT_TIME FROM tm_visitor a  LEFT JOIN tm_belonging b ON b.BID=a.ID LEFT JOIN tm_exit c ON c.ID=a.ID limit "+i+",10";

System.out.println(sql);
ResultSet rs=st.executeQuery(sql);
while(rs.next())
    {
    ar.add(rs.getInt("ID"));
    ar.add(rs.getObject("VISITOR_TEXT"));
    ar.add(rs.getObject("COMPANY_TEXT"));
    ar.add(rs.getObject("CONTACT"));
    ar.add(rs.getObject("PERSON_TO_MEET"));
    ar.add(rs.getObject("DEPARTMENT_TEXT"));
    ar.add(rs.getObject("FLOOR"));
    ar.add(rs.getObject("BELONGING_TYPE"));
    ar.add(rs.getObject("BELONGING_TEXT"));
    ar.add(rs.getObject("INTIME"));
    ar.add(rs.getObject("EXIT_TIME"));

    }

<tr>
    <td align="center"><input type="text" name="id" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="visitorname" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="company" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="contact" value="<%=itr.next()%>" readonly size="10"></td>
    <td align="center"><input type="text" name="whometomeet" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="department" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="floor" value="<%=itr.next()%>" readonly size="7"></td>
    <td align="center"><input type="text" name="btype" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="bel" value="<%=itr.next()%>" readonly size="15"></td>
    <td align="center"><input type="text" name="intime" value="<%=itr.next()%>" readonly size="20"></td>
    <td align="center"><input type="text" name="exittime" value="<%=itr.next()%>" readonly size="20"></td>



</tr>

执行上述查询后,如果tm_exit.EXIT_TIME中的值不存在则在JSP表文本框中显示NULL如何避免此

我试过了:

<%!
String blanknull(String s) {
  return (s == null) ? "" : s;
}
%>

但没有用,请帮帮忙?

这是我用来获取数据的查询

SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,a.DEPARTMENT_TEXT,‌​a.FLOOR,b.BELONG ING_TYPE,b.BELONGING_TEXT ,a.INTIME, c.(ifnull(EXIT_TIME,'NA')) as EXIT_TIME FROM tm_visitor a LEFT JOIN tm_b elonging b ON b.BID=a.ID LEFT JOIN tm_exit c ON c.ID=a.ID; 

但是会出现以下错误

错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'(ifnull(EXIT_TIME,'NA'))附近使用正确的语法作为EXIT_TIME FROM tm_visitor a LEFT JOI N tm_belonging

1 个答案:

答案 0 :(得分:0)

你可以避免空值,使用MySQL的ifnull函数,

具有空值的表:

mysql> select * from test;
+------+---------+
| id   | name    |
+------+---------+
|    1 | Jcho360 |
|    1 | NULL    |
+------+---------+
2 rows in set (0.00 sec)

具有IFNULL功能的表

mysql> select id, ifnull(name,'Its NOT NULL')as name from test;
+------+--------------+
| id   | name         |
+------+--------------+
|    1 | Jcho360      |
|    1 | Its NOT NULL |
+------+--------------+
2 rows in set (0.00 sec)

而不是'not NOT NULL'你可以放''或其他任何东西,你不会再有null

编辑:

尝试

String sql="SELECT a.ID,a.VISITOR_TEXT,a.COMPANY_TEXT,a.CONTACT,a.PERSON_TO_MEET,
a.DEPARTMENT_TEXT,a.FLOOR,b.BELONGING_TYPE,b.BELONGING_TEXT ,a.INTIME,
 ifnull(c.EXIT_TIME,0) FROM tm_visitor a  
LEFT JOIN tm_belonging b ON b.BID=a.ID 
LEFT JOIN tm_exit c ON c.ID=a.ID limit "+i+",10";