我有这样的表 -
fnumber|fstatus|department|date&time
f1 out d1 a
f2 out d2 b
f1 in d3 c
f1 out d3 d
f2 in d1 e
f1 in d1 f
f2 out d1 g
f2 in d2 h
有没有一种方法可以让同一个fnumber自动组合在一起并根据日期和时间(从旧到新)排列 -
fnumber|fstatus|department|date&time
f1 out d1 a
f1 in d3 c
f1 out d3 d
f1 in d1 f
f2 out d2 b
f2 in d1 e
f2 out d1 g
f2 in d2 h
现在我正在使用servlet和jsp来简单地插入值,但我希望它们像上面一样排列(假设我插入f1然后f2然后f3现在如果f1再次插入我希望它插入f1和f2之间不是在f3之后。 在某种程度上我能做到这一点。
fileStatus.jsp这是用户提交值的文件。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Status Page</title>
<style>
header {
background-color:teal;
color:white;
text-align:center;
padding:30px;
}
section {
width:350px;
float:left;
padding:150px;
}
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
<%
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if(cookies !=null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("admin")) userName = cookie.getValue();
}
}
%>
<header>
<h3>Hi <%=userName %></h3>
</header>
<a href="create.jsp"><font color="black">back</font></a>
<form action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<section>
<h3>Change Status</h3>
<form action="statusServlet" method="post">
<table>
<tbody>
<tr>
<td>
File Number :<select name="files">
<%
try{
String sql="select * from files";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
"root", "root");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
%>
<option value="<%=rs.getString("fileno")%>"><%=rs.getString("fileno")%></option>
<%}
rs.close();
st.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
</select></td>
</tr>
<tr>
<td>
File Department :<select name="departments">
<%
try{
String sql="select * from department";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
"root", "root");
Statement st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
%>
<option value="<%=rs.getString("departmentname")%>"><%=rs.getString("departmentname")%></option>
<%}
rs.close();
st.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
</select></td>
</tr>
<tr>
<td>
File Status :<select name="input">
<option>IN</option>
<option>OUT</option>
</select></td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="submit" />
</td>
</tr>
</tbody>
</table>
</form>
</section>
<footer>
Copyright example. All right reserved.
</footer>
</body>
</html>
stausServlet.java在此我使用sql查询来插入数据。
package bean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class statusServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("JSESSIONID")){
System.out.println("JSESSIONID="+cookie.getValue());
break;
}
}
}
HttpSession session = request.getSession(false);
System.out.println("admin="+session.getAttribute("admin"));
if(session!=null && session.getAttribute("admin") != null){
String user=(String)session.getAttribute("admin");
boolean status=false;
try{
String fno=request.getParameter("files");
String departments=request.getParameter("departments");
String input=request.getParameter("input");
Connection con=ConnectionProvider.getCon();
String sql="insert into status(fnumber,fstatus,department) values (?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
pstmt.setString(1,fno);
pstmt.setString(2,input);
pstmt.setString(3,departments);
int rs=pstmt.executeUpdate();
if(rs>0){status=true;}
}catch(Exception e){}
if(status){
response.sendRedirect("fileStatus.jsp");
PrintWriter out= response.getWriter();
out.println("Values have been inserted,"+user);
//out.flush();
}
else
{
PrintWriter out= response.getWriter();
out.println("failed");
response.sendRedirect("create.jsp");
}
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.html");
PrintWriter out= response.getWriter();
out.println("<font color=red>Either user name or password is wrong.</font>");
rd.include(request, response);
}
}
}
答案 0 :(得分:1)
如果您使用sql获取数据,则应使用Order By
子句。
如果您使用的是Java列表,则可以使用Comparator
对列表进行排序,例如参见https://stackoverflow.com/a/18441978/3543153