1

I have table like 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

Is there a way such that same fnumber are automatically be together and arranged according to date and time(from old to new) like 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

right now i am using servlet and jsp for simply inserting the value but i want them to get arranged like above (Suppose I insert f1 then f2 then f3 now if f1 is inserted again i want it to be inserted between f1 and f2 not after f3. Is ther some way i could do that.

fileStatus.jsp This is the file where user submits the value.

<%@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 In this i am using sql query to insert the data.

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

}

}
Sushant Baweja
  • 202
  • 1
  • 5
  • 14
  • 2
    You could sort while you query the data. – 4J41 Jun 24 '16 at 12:32
  • ´String sql="select * from department";´ should be ´String sql="select * from department ORDER BY fnumber asc;";´ and your table is ordered. "Inserting between" values is nothing anyone does. – mondjunge Jun 24 '16 at 13:57

1 Answers1

1

If you are using sql to get your data you should use the Order By clause.

If you are using a java List, you can sort the List by using a Comparator, see for example https://stackoverflow.com/a/18441978/3543153

Community
  • 1
  • 1
DamienB
  • 419
  • 1
  • 6
  • 20
  • i don't really understand... The data you are displaying at the beginning of your question are the data inside the table of your database? Or is it the view you are expecting to get in a jsp file? – DamienB Jun 24 '16 at 12:58
  • itis the data inside the table in my database – Sushant Baweja Jun 24 '16 at 13:00
  • The datas inside the table can't be reorder. Or i don't know the solution to do it. But you can sort them in a SELECT statement by using the order by clause – DamienB Jun 24 '16 at 13:03
  • I have removed the first paragraph which looked more like a request for more information (and therefor should have been a comment). – Mark Rotteveel Jun 25 '16 at 06:56