Java简单示例-用户登录、单个页面的增删改查及简单分页

 index.html  -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到stulist.jsp)

stulist.jsp 实现对学生的增删改查及分页.(stulist.jsp检测是否有session,以及student列表中是否有学生信息,没有session返回index页面,student列表中没有信息调用GetStudentServlet获取学生信息,获取过程中已分页)

数据库操作类详见:http://www.cnblogs.com/A--Q/p/6137525.html

 stulist.jsp界面如下:

查询:可以实现组合查询,输入条件,点击‘查询’即可

增加:在上方输入完整学生信息后,选择‘添加’选项按钮,添加可用,点击‘添加’,即将刚才填写的学生信息,增加的数据库中

删除:在要删除的学生后点击‘删除’,弹出‘询问框’,是否要删除该学生信息,选择‘是’从数据库中删除该学生,选择‘否’取消对该学生的删除操作。

修改:在要修改的学生后点击‘修改’,表格上方,显示一排信息框,除学号外,填写好更改的信息,点击更新,即可,或者点击‘取消’,取消本次的更新。

(如果删除了该学生,则该页面会刷新,上方的更新框会隐藏,即不会存在逻辑性错误)。

 

对于分页:

 这里没有让用户输入每页显示多少条信息,而是笔者自己再代码中写好的,每页显示10条,如果没有条件则显示如下:

如果加上条件,则显示为:

在stulist.jsp 页面中共有两个表单,一个表单传入到 GetStudentServlet 执行stulist.jsp传入的数据进行分页跳转,插入输入,查询数据,flag=3 分页跳转 flag=2插入 flag=1查询,一个表单传入到GetStudentServlet_extend 执行stulist.jsp传入的数据进行删除和修改数据,flag=1 删除 flag=2修改

以上两个Servlet操作完后都会返回student数组列表对象在stulist显示。(使用StudentDao对象得到student数组列表)

StudentDao:

 1 package com.mis.dao;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.util.ArrayList;
 6 import com.mis.bean.Student;
 7 
 8 public class StudentDao extends DBOper{
 9     public ArrayList<Student> getStudent(int flag,String parameter,int crrentPage){//分页查询
10         String sql="select id,stuno,name,sex,age,major from student order by id desc";
11         if (flag>0) {
12             sql= "select * from student where "+parameter;
13         }
14         crrentPage=(crrentPage==0?0:crrentPage-1);
15         sql+=" limit "+crrentPage*10+",10";//默认每页显示
16         ArrayList<Student> students = new ArrayList<Student>();
17         ResultSet rs = executeQuery(sql,null);
18         try {
19             while(rs.next()){
20                 Student s = new Student();
21                 s.setId(rs.getInt("id"));
22                 s.setStuno(rs.getString("stuno"));
23                 s.setName(rs.getString("name"));
24                 s.setSex(rs.getString("sex"));
25                 s.setAge(rs.getInt("age"));
26                 s.setMajor(rs.getString("major"));
27                 students.add(s);
28             }
29         } catch (SQLException e) {
30             e.printStackTrace();
31         }
32         return students;    
33     }
34     public int getStudentCount(int flag,String parameter){//得到查询的总条数
35         String sql="select id,stuno,name,sex,age,major from student";
36         if (flag>0) {
37             sql= "select * from student where "+parameter;
38         }
39         ResultSet rs = executeQuery(sql,null);
40         try {
41             rs.last();
42             return rs.getRow();
43         } catch (SQLException e) {
44             return 0;
45         }
46     }
47     public ArrayList<Student> getStudent(int flag,String parameter){//未分页查询
48         String sql="select id,stuno,name,sex,age,major from student order by id";
49         if (flag>0) {
50             sql= "select * from student where "+parameter;
51         }
52         ArrayList<Student> students = new ArrayList<Student>();
53         ResultSet rs = executeQuery(sql,null);
54         try {
55             while(rs.next()){
56                 Student s = new Student();
57                 s.setId(rs.getInt("id"));
58                 s.setStuno(rs.getString("stuno"));
59                 s.setName(rs.getString("name"));
60                 s.setSex(rs.getString("sex"));
61                 s.setAge(rs.getInt("age"));
62                 s.setMajor(rs.getString("major"));
63                 students.add(s);
64             }
65         } catch (SQLException e) {
66             e.printStackTrace();
67         }
68         return students;    
69     }
70     public int addDelUp(String sql) {
71         int num=0;
72         num=executeUpdate(sql,null);
73         if(num!=0)return num;
74         return 0;
75     }    
76 }

GetStudentServlet_extend

 1 package com.mis.servlet;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import com.mis.bean.Student;
13 import com.mis.dao.StudentDao;
14 
15 public class GetStudentServlet_extend extends HttpServlet {
16     private static final long serialVersionUID = 1L;
17 
18     public GetStudentServlet_extend() {
19         super();
20     }
21 
22     // 执行stulist.jsp传入的数据进行删除和修改数据,flag=1 删除 flag=2修改
23     protected void doGet(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25         request.setCharacterEncoding("utf-8");
26         response.setContentType("text/html;charset=utf-8");
27         ServletContext ctx = this.getServletContext();
28         String server = ctx.getInitParameter("server");
29         String dbname = ctx.getInitParameter("dbname");
30         String user = ctx.getInitParameter("user");
31         String pwd = ctx.getInitParameter("pwd");
32         StudentDao dao = new StudentDao();
33         String sql = null;
34         int flag = 0;
35         try {
36             flag = Integer.parseInt(request.getParameter("flag_delUp"));
37         } catch (Exception e) {
38             flag = 0;
39         }
40         String stuid = request.getParameter("ud");
41         String stuname = request.getParameter("ud_name");
42         String stusex = request.getParameter("ud_sex");
43         String stuage = request.getParameter("ud_age");
44         String stumajor = request.getParameter("ud_major");
45         String del_stuid = request.getParameter("parameter_del");
46         if (flag == 1) {// 执行删除
47             try {
48                 flag = 0;
49                 sql = "Delete from student where stuno='" + del_stuid + "';";
50                 dao.getConn(server, dbname, user, pwd);
51                 dao.addDelUp(sql);
52             } catch (Exception e) {
53                 e.printStackTrace();
54             }
55         } else if (flag == 2) {// 执行修改
56             flag = 0;
57             sql = "update student set name='" + stuname + "',sex='" + stusex + "',age='" + stuage + "',major='"
58                     + stumajor + "' where stuno='" + stuid + "';";
59             try {
60                 dao.getConn(server, dbname, user, pwd);
61                 dao.addDelUp(sql);
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66         try {
67             dao.getConn(server, dbname, user, pwd);
68             ArrayList<Student> students = dao.getStudent(0, null, 0);
69             request.setAttribute("stuCount", dao.getStudentCount(0, null));
70             request.setAttribute("students", students);
71             request.setAttribute("currentPage", 1);//默认用户打开的分页第一页
72             request.getRequestDispatcher("stulist.jsp").forward(request, response);
73             return;
74         } catch (Exception e) {
75             e.printStackTrace();
76         }
77 
78     }
79 
80     protected void doPost(HttpServletRequest request, HttpServletResponse response)
81             throws ServletException, IOException {
82         doGet(request, response);
83     }
84 }

stulist.jsp

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <%@ page import="java.util.*"%>
  4 <%@ page import="java.lang.Math"%>
  5 <%@ page import="com.mis.bean.Student"%>
  6 <%@ page import="com.mis.dao.DBOper"%>
  7 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  8 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
  9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 10 <html>
 11 <head>
 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 13 <title>学生信息列表</title>
 14 
 15 <script src="js_jquery/stulist.js" charset="UTF-8"></script>
 16 <link rel="stylesheet" href="css/stulist.css">
 17 </head>
 18 <body>
 19     <c:if test="${username==null }">
 20         <jsp:forward page="index.html"></jsp:forward>
 21     </c:if>
 22     <c:if test="${students==null}">
 23         <jsp:forward page="getStudent"></jsp:forward>
 24     </c:if>
 25 
 26     <table border="0" align="center" cellspacing="0" class="list_table"
 27         id="senfe" style='width: 90%'>
 28         <thead>
 29             <tr>
 30                 <td colspan='7'>
 31                     <form method="POST" name="form" action="getStudent">
 32                         <!-- 开发阶段 没有对用户的非法输入进行过滤-->
 33                         <input type="hidden" name="flag" value="1"> <input
 34                             type="text" name="stuid" value="学号"
 35                             onclick="if(this.value == '学号')this.value ='';"
 36                             onBlur="if(this.value == ' '||this.value == '')this.value ='学号';">
 37                         <input type="text" name="stuname" value="姓名"
 38                             onclick="if(this.value == '姓名')this.value ='';"
 39                             onBlur="if(this.value == ' '||this.value == '')this.value ='姓名';">
 40                         <input type="text" name="stusex" value="性别"
 41                             onclick="if(this.value == '性别')this.value ='';"
 42                             onBlur="if(this.value == ' '||this.value == '')this.value ='性别';">
 43                         <input type="text" name="stuage" value="年龄"
 44                             onclick="if(this.value == '年龄')this.value ='';"
 45                             onBlur="if(this.value == ' '||this.value == '')this.value ='年龄';">
 46                         <input type="text" name="stumajor" value="专业"
 47                             onclick="if(this.value == '专业')this.value ='';"
 48                             onBlur="if(this.value == ' '||this.value == '')this.value ='专业';">
 49                         <input type="radio" name="selAdd" checked onclick="selop()">
 50                         <input type="submit" value="查询" id="selcon"> <input
 51                             type="radio" name="selAdd" onclick="selop()"><input
 52                             type="submit" disabled value="增加" id="addcon" onclick="setPar()">
 53                     </form>
 54                 </td>
 55             </tr>
 56             <tr>
 57                 <th>学号</th>
 58                 <th>姓名</th>
 59                 <th>性别</th>
 60                 <th>年龄</th>
 61                 <th>专业</th>
 62                 <th>删除</th>
 63                 <th>修改</th>
 64             </tr>
 65         </thead>
 66         <tbody>
 67             <form method="post" name="DelUp" action="getStudent_extend">
 68                 <c:forEach var="student" items="${students }">
 69                     <tr align="center">
 70                         <td>${student.stuno }</td>
 71                         <td>${student.name }</td>
 72                         <td>${student.sex }</td>
 73                         <td>${student.age }</td>
 74                         <td align="left">${student.major }</td>
 75                         <td><input type="submit" name=${student.stuno } value="删除"
 76                             onclick="del(this)"></td>
 77                         <td><input type="button" name=${student.stuno } value="修改"
 78                             onclick="updata(this)"></td>
 79                     </tr>
 80                 </c:forEach>
 81                 
 82                     <div id="updateDiv">
 83                     学号:<input type="text" name="ud" readOnly>
 84                     姓名:<input type="text" name="ud_name">
 85                     性别:<input type="text" name="ud_sex">
 86                     年龄:<input type="text" name="ud_age">
 87                     专业:<input type="text" name="ud_major">
 88                     
 89                     <input type="submit" value="更新">
 90                     <span id="cancelUpdate" onclick="cancelupdate()">取消</span>
 91                     </div>
 92                     
 93                 <input type="hidden" name="flag_delUp"> <input type="hidden"
 94                     name="parameter_del">
 95             </form>
 96         </tbody>
 97     </table>
 98     <div id="pageCount" style="width:600px;margin-left:auto;margin-right:auto;">
 99         <c:if test="${stuCount!=null}">
100             <br /><c:if test="${condition!=null&&condition!=''}">查询条件为<c:out value="${condition}"></c:out></c:if>
101             <c:out value="${stuCount}"></c:out>条记录,<!-- Math.ceil() 向上取整  -- (stuCount-((stuCount-1)%10))/10+1 ** -->
102<fmt:formatNumber type="number" value="${(stuCount-((stuCount-1)%10))/10+1}" maxFractionDigits="0"/>103             ,当前为第<c:out value="${currentPage}"></c:out>104         
105         <form method="post" name="paging" action="getStudent">
106             <input type="text" value="" name="page" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
107             <input type="hidden" name="flag" value="3">
108             <input type="hidden" name="parame" value='<c:out value="${condition}"></c:out>'>
109              <input    type="submit" value="跳转" onclick="checkPage()">
110         </form>
111     </c:if>
112     </div>
113 </body>
114 </html>

 源代码:https://pan.baidu.com/s/1e_mTr1Rf4BoehNBwDEg76g 密码:8qg3

posted @ 2016-12-06 15:18  马丁黄瓜啊  阅读(34033)  评论(4编辑  收藏  举报