2016-06-24 7 views
1

私はthis-プット行

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が自動的this-のように(古いから新しいに)日付と時間に応じて一緒に配置することがされているような方法はあります

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 

今(私は、F2キーをF1キーを挿入したと私は単純に値を挿入するためのサーブレットとJSPを使用していますが、私は彼らが上記のように配置された取得したいf1が再び挿入されているならば、f3は、今私はそれが挿入されるようにしたいですf1とf2との間にf3の後でない。 ther some wa私はそれをすることができました。

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); 
    } 

} 

} 
+2

のためにあなたが並べ替えることができ参照、Comparatorを使用して、リストを並べ替えることができます。 – 4J41

+0

'string sql = "部門から選択*;' 'は' ORDER BY fnumber asc; department 'の'String sql =" select *でなければなりません。 「間に挿入する」という値は誰のものでもありません。 – mondjunge

答えて

1

sqlを使用してデータを取得する場合は、Order By句を使用する必要があります。

あなたはJavaのリストを使用している場合は、あなたがデータを照会しながら、例https://stackoverflow.com/a/18441978/3543153

+0

私は質問を編集しました –

+0

私は本当に理解していません...あなたの質問の冒頭に表示されているデータは、あなたのデータベースのテーブル内のデータですか?それとも、あなたがJSPファイルに収めようとしているビューですか? – DamienB

+0

私のデータベースのテーブル内のデータ –

関連する問題