servlet声明定义message信息传给前天界面判断输出message;
if(booknamelist.size()==0) {
message="根据书名查询没有结果!";
}
<%
String message = (String)request.getAttribute("message");
if(message!=null){
%>
<%} %>
查询方法可以这么写:
前台:
复制代码
1
2
3
4
5
6
13
16
17
14 查询
15
18
19
复制代码
不同的查询方式对应着不同option的不同value值,这样可以在servlet里面更加清晰地定义不同SQL语句进行一次模糊查询,只需要写一个查询函数就可以了!
servlet:
复制代码
1 public ArrayList select(String content,String way){
2 ArrayList list=new ArrayList<>();
3 Connection con=null;
4 Statement state=null;
5 ResultSet rs=null;
6 String sql="select * from student";
7 if(content!=""&&"1".equals(way)){
8 sql +=" where sno='"+content+"'";
9 }else if(content!=""&&"2".equals(way)){
10 sql += " where sname like'%" + content + "%'";
11 }else if(content!=""&&"3".equals(way)){
12 sql += " where ssex='"+content+"'";
13 }else if(content!=""&&"4".equals(way)){
14 sql += " where saddress='"+content+"'";
15 }else{
16 sql ="select * from student";
17 }
18 try {
19 con=DBUtil.getConn();
20 state=con.createStatement();
21 rs=state.executeQuery(sql);
22 while(rs.next())
23 {
24 User bean=new User();
25 bean.setSno(rs.getString("sno"));
26 bean.setSname(rs.getString("sname"));
27 bean.setSsex(rs.getString("ssex"));;
28 bean.setSbirthday(rs.getString("sbirthday"));
29 bean.setSaddress(rs.getString("saddress"));
30 list.add(bean);
31 }
32
33 } catch (SQLException e) {
34 // TODO Auto-generated catch block
35 e.printStackTrace();
36 }
37 DBUtil.close(rs, state, con);
38 return list;
39 }