您的位置:首页 > 数据库

第一次使用博客 贴一个JSP+SQL实现按日期查询留言的代码吧~

2017-11-21 17:52 603 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjff.dao.MatesDao"%>
<%@page import="com.zjff.dao.impl.MatesDaoimpl"%>
<%@page import="com.zjff.entity.Mates"%>
<%@page import="com.zjff.dao.MessageTypeDao"%>
<%@page import="com.zjff.dao.impl.MessageTypeDaoimpl"%>
<%@page import="com.zjff.dao.MateTypeDao"%>
<%@page import="com.zjff.dao.impl.MateTypeDaoimpl"%>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
#header {
background-color: #CCC;
height: 120px;
width: 100%;
text-align: center;
font: 微软雅黑;
line-height: 120px;
letter-spacing: 5px;
font-size: 24px;
}

#nav {
height: 50px;
width: 100%;
text-align: center;
line-height: 50px;
margin-top: 5px;
}

</style>

</head>
<%
MatesDao mate = new MatesDaoimpl();
List<Mates> list = new ArrayList<Mates>();
list = mate.getAllMate(); //输入所有留言的实例 用了SQL select * form mate
Set<Date> set =new HashSet<Date>();
for(int i=0;i<list.size();i++){
set.add(list.get(i).getSubDate()); //用set集合特性 将重复的日期唯一化
}
%>
<body bgcolor="#7FFFD4">
<div id="header">
班级留言板
</div>
<div id="nav">
<form action="check.jsp" method="post">
<select name="subtime">
<%

for(Date time:set ){ //遍历set集合
%>
<option value="<%=time%>"><%=time%></option> //将value直接取值 提交至下个页面赋值容易
<%

}
%>
</select>

<button type="submit" value="Submit">
查询
</button>

<button>
<a href="addit.jsp" style="text-decoration: none;color: black;">添加</a>
</button>
</form>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 "
id="content">

<div class="content_table">
<table width="100%" cellpadding="0" cellspacing="1"
bgcolor="#dedede">
<caption>
table
</caption>
<tr bgcolor="#eae9ca">
<th>
ID
</th>
<th>
用戶姓名
</th>
<th>
留言內容
</th>
<th>
留言时间
</th>
<th>
留言类型
</th>
<th>
用户类型
</th>

</tr>
<%
Mates mates=null;
MessageTypeDao mad=new MessageTypeDaoimpl();
MateTypeDao mtd=new MateTypeDaoimpl();
for(int i=0;i<list.size();i++){
mates=list.get(i); //遍历所有留言

%>
<% if(i%2==0){%>
<tr class="odd">
<td>
<%=mates.getMateId() %>
</td>
<td>
<%=mates.getMateName() %>
</td>
<td>
<%=mates.getMateContent() %>

</td>
<td>
<%=mates.getSubDate() %>
</td>
<td>
<%=mad.getMessageTypeById(mates.getMessageTypeId()).getMessageTypeName() %>
</td>
<td>
<%if(mates.getMateTypeId()!=0){ %>
<%=mtd.getMateTypeById(mates.getMateTypeId()).getMateTypeName() %>
<% }else{%>
<%="一般同学" %>
<%} %>
</td>

</tr>
<%}else{ %>
<tr class="even">
<td>
<%=mates.getMateId() %>
</td>
<td>
<%=mates.getMateName() %>
</td>
<td>
<%=mates.getMateContent() %>

</td>
<td>
<%=mates.getSubDate() %>
</td>
<td>
<%=mad.getMessageTypeById(mates.getMessageTypeId()).getMessageTypeName() %>
</td>
<td>
<%if(mates.getMateTypeId()!=0){ %>
<%=mtd.getMateTypeById(mates.getMateTypeId()).getMateTypeName() %>
<% }else{%>
<%="一般同学" %>
<%} %>
</td>

</tr>
<%} }%>
</table>
</div>
<div class="page">
<div class="right">

</div>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zjff.dao.MatesDao"%>
<%@page import="com.zjff.dao.impl.MatesDaoimpl"%>
<%@page import="com.zjff.entity.Mates"%>
<%@page import="com.zjff.dao.MessageTypeDao"%>
<%@page import="com.zjff.dao.impl.MessageTypeDaoimpl"%>
<%@page import="com.zjff.dao.MateTypeDao"%>
<%@page import="com.zjff.dao.impl.MateTypeDaoimpl"%>
<%@page import="java.util.logging.SimpleFormatter"%>
<%@page import="java.text.SimpleDateFormat"%>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
#header {
background-color: #CCC;
height: 120px;
width: 100%;
text-align: center;
font: 微软雅黑;
line-height: 120px;
letter-spacing: 5px;
font-size: 24px;
}

#nav {
height: 50px;
width: 100%;
text-align: center;
line-height: 50px;
margin-top: 5px;
}

</style>
 
</head>
<%
MatesDao mate = new MatesDaoimpl();
List<Mates> list = new ArrayList<Mates>();
Date d=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("subtime"));//将上个界面提交的值转成Date类型
list=mate.getMessageBySubDate(d);
Set<Date> set =new HashSet<Date>();
for(int i=0;i<list.size();i++){
set.add(list.get(i).getSubDate());
}

%>
<body bgcolor="#7FFFD4">
<div id="header">
班级留言板
</div>
<div id="nav">

<button>
<a href="find.jsp" style="text-decoration: none;color: black;">返回</a>
</button>

<button>
<a href="addit.jsp" style="text-decoration: none;color: black;">添加</a>
</button>

</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 "
id="content">

<div class="content_table">
<table width="100%" cellpadding="0" cellspacing="1"
bgcolor="#dedede">
<caption> 
table 
</caption>
<tr bgcolor="#eae9ca">
<th>
ID
</th>
<th>
用戶姓名
</th>
<th>
留言內容
</th>
<th>
留言时间
</th>
<th>
留言类型
</th>
<th>
用户类型
</th>

</tr>
<%
Mates mates=null;
MessageTypeDao mad=new MessageTypeDaoimpl();
MateTypeDao mtd=new MateTypeDaoimpl(); 
for(int i=0;i<list.size();i++){ 
mates=list.get(i);

%>
<% if(i%2==0){%>
<tr class="odd">
<td>
<%=mates.getMateId() %>
</td>
<td>
<%=mates.getMateName() %>
</td>
<td>
<%=mates.getMateContent() %>

</td>
<td>
<%=mates.getSubDate() %>
</td>
<td>
<%=mad.getMessageTypeById(mates.getMessageTypeId()).getMessageTypeName() %>
</td>
<td>
<%if(mates.getMateTypeId()!=0){ %>
<%=mtd.getMateTypeById(mates.getMateTypeId()).getMateTypeName() %>
<% }else{%>
<%="一般同学" %>
<%} %>
</td>

</tr>
<%}else{ %>
<tr class="even">
<td>
<%=mates.getMateId() %>
</td>
<td>
<%=mates.getMateName() %>
</td>
<td>
<%=mates.getMateContent() %>

</td>
<td>
<%=mates.getSubDate() %>
</td>
<td>
<%=mad.getMessageTypeById(mates.getMessageTypeId()).getMessageTypeName() %>
</td>
<td>
<%if(mates.getMateTypeId()!=0){ %>
<%=mtd.getMateTypeById(mates.getMateTypeId()).getMateTypeName() %>
<% }else{%>
<%="一般同学" %>
<%} %>
</td>

</tr>
<%} }%>
</table>
</div>
<div class="page">
<div class="right">

</div>
</div>
</div>
</body>
</html>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp sql