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

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界面如下:

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

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

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

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

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

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

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

对于分页:

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

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

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

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

在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:

 package com.mis.dao;

 import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.mis.bean.Student; public class StudentDao extends DBOper{
public ArrayList<Student> getStudent(int flag,String parameter,int crrentPage){//分页查询
String sql="select id,stuno,name,sex,age,major from student order by id desc";
if (flag>0) {
sql= "select * from student where "+parameter;
}
crrentPage=(crrentPage==0?0:crrentPage-1);
sql+=" limit "+crrentPage*10+",10";//默认每页显示
ArrayList<Student> students = new ArrayList<Student>();
ResultSet rs = executeQuery(sql,null);
try {
while(rs.next()){
Student s = new Student();
s.setId(rs.getInt("id"));
s.setStuno(rs.getString("stuno"));
s.setName(rs.getString("name"));
s.setSex(rs.getString("sex"));
s.setAge(rs.getInt("age"));
s.setMajor(rs.getString("major"));
students.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public int getStudentCount(int flag,String parameter){//得到查询的总条数
String sql="select id,stuno,name,sex,age,major from student";
if (flag>0) {
sql= "select * from student where "+parameter;
}
ResultSet rs = executeQuery(sql,null);
try {
rs.last();
return rs.getRow();
} catch (SQLException e) {
return 0;
}
}
public ArrayList<Student> getStudent(int flag,String parameter){//未分页查询
String sql="select id,stuno,name,sex,age,major from student order by id";
if (flag>0) {
sql= "select * from student where "+parameter;
}
ArrayList<Student> students = new ArrayList<Student>();
ResultSet rs = executeQuery(sql,null);
try {
while(rs.next()){
Student s = new Student();
s.setId(rs.getInt("id"));
s.setStuno(rs.getString("stuno"));
s.setName(rs.getString("name"));
s.setSex(rs.getString("sex"));
s.setAge(rs.getInt("age"));
s.setMajor(rs.getString("major"));
students.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public int addDelUp(String sql) {
int num=0;
num=executeUpdate(sql,null);
if(num!=0)return num;
return 0;
}
}

GetStudentServlet_extend

 package com.mis.servlet;

 import java.io.IOException;
import java.util.ArrayList; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.mis.bean.Student;
import com.mis.dao.StudentDao; public class GetStudentServlet_extend extends HttpServlet {
private static final long serialVersionUID = 1L; public GetStudentServlet_extend() {
super();
} // 执行stulist.jsp传入的数据进行删除和修改数据,flag=1 删除 flag=2修改
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ServletContext ctx = this.getServletContext();
String server = ctx.getInitParameter("server");
String dbname = ctx.getInitParameter("dbname");
String user = ctx.getInitParameter("user");
String pwd = ctx.getInitParameter("pwd");
StudentDao dao = new StudentDao();
String sql = null;
int flag = 0;
try {
flag = Integer.parseInt(request.getParameter("flag_delUp"));
} catch (Exception e) {
flag = 0;
}
String stuid = request.getParameter("ud");
String stuname = request.getParameter("ud_name");
String stusex = request.getParameter("ud_sex");
String stuage = request.getParameter("ud_age");
String stumajor = request.getParameter("ud_major");
String del_stuid = request.getParameter("parameter_del");
if (flag == 1) {// 执行删除
try {
flag = 0;
sql = "Delete from student where stuno='" + del_stuid + "';";
dao.getConn(server, dbname, user, pwd);
dao.addDelUp(sql);
} catch (Exception e) {
e.printStackTrace();
}
} else if (flag == 2) {// 执行修改
flag = 0;
sql = "update student set name='" + stuname + "',sex='" + stusex + "',age='" + stuage + "',major='"
+ stumajor + "' where stuno='" + stuid + "';";
try {
dao.getConn(server, dbname, user, pwd);
dao.addDelUp(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
dao.getConn(server, dbname, user, pwd);
ArrayList<Student> students = dao.getStudent(0, null, 0);
request.setAttribute("stuCount", dao.getStudentCount(0, null));
request.setAttribute("students", students);
request.setAttribute("currentPage", 1);//默认用户打开的分页第一页
request.getRequestDispatcher("stulist.jsp").forward(request, response);
return;
} catch (Exception e) {
e.printStackTrace();
} } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

stulist.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.Math"%>
<%@ page import="com.mis.bean.Student"%>
<%@ page import="com.mis.dao.DBOper"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息列表</title> <script src="js_jquery/stulist.js" charset="UTF-8"></script>
<link rel="stylesheet" href="css/stulist.css">
</head>
<body>
<c:if test="${username==null }">
<jsp:forward page="index.html"></jsp:forward>
</c:if>
<c:if test="${students==null}">
<jsp:forward page="getStudent"></jsp:forward>
</c:if> <table border="0" align="center" cellspacing="0" class="list_table"
id="senfe" style='width: 90%'>
<thead>
<tr>
<td colspan='7'>
<form method="POST" name="form" action="getStudent">
<!-- 开发阶段 没有对用户的非法输入进行过滤-->
<input type="hidden" name="flag" value="1"> <input
type="text" name="stuid" value="学号"
onclick="if(this.value == '学号')this.value ='';"
onBlur="if(this.value == ' '||this.value == '')this.value ='学号';">
<input type="text" name="stuname" value="姓名"
onclick="if(this.value == '姓名')this.value ='';"
onBlur="if(this.value == ' '||this.value == '')this.value ='姓名';">
<input type="text" name="stusex" value="性别"
onclick="if(this.value == '性别')this.value ='';"
onBlur="if(this.value == ' '||this.value == '')this.value ='性别';">
<input type="text" name="stuage" value="年龄"
onclick="if(this.value == '年龄')this.value ='';"
onBlur="if(this.value == ' '||this.value == '')this.value ='年龄';">
<input type="text" name="stumajor" value="专业"
onclick="if(this.value == '专业')this.value ='';"
onBlur="if(this.value == ' '||this.value == '')this.value ='专业';">
<input type="radio" name="selAdd" checked onclick="selop()">
<input type="submit" value="查询" id="selcon"> <input
type="radio" name="selAdd" onclick="selop()"><input
type="submit" disabled value="增加" id="addcon" onclick="setPar()">
</form>
</td>
</tr>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>专业</th>
<th>删除</th>
<th>修改</th>
</tr>
</thead>
<tbody>
<form method="post" name="DelUp" action="getStudent_extend">
<c:forEach var="student" items="${students }">
<tr align="center">
<td>${student.stuno }</td>
<td>${student.name }</td>
<td>${student.sex }</td>
<td>${student.age }</td>
<td align="left">${student.major }</td>
<td><input type="submit" name=${student.stuno } value="删除"
onclick="del(this)"></td>
<td><input type="button" name=${student.stuno } value="修改"
onclick="updata(this)"></td>
</tr>
</c:forEach> <div id="updateDiv">
学号:<input type="text" name="ud" readOnly>
姓名:<input type="text" name="ud_name">
性别:<input type="text" name="ud_sex">
年龄:<input type="text" name="ud_age">
专业:<input type="text" name="ud_major"> <input type="submit" value="更新">
<span id="cancelUpdate" onclick="cancelupdate()">取消</span>
</div> <input type="hidden" name="flag_delUp"> <input type="hidden"
name="parameter_del">
</form>
</tbody>
</table>
<div id="pageCount" style="width:600px;margin-left:auto;margin-right:auto;">
<c:if test="${stuCount!=null}">
<br /><c:if test="${condition!=null&&condition!=''}">查询条件为<c:out value="${condition}"></c:out></c:if>
<c:out value="${stuCount}"></c:out>条记录,<!-- Math.ceil() 向上取整 -- (stuCount-((stuCount-1)%10))/10+1 ** -->
共<fmt:formatNumber type="number" value="${(stuCount-((stuCount-1)%10))/10+1}" maxFractionDigits="0"/>页
,当前为第<c:out value="${currentPage}"></c:out>页 <form method="post" name="paging" action="getStudent">
<input type="text" value="" name="page" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
<input type="hidden" name="flag" value="3">
<input type="hidden" name="parame" value='<c:out value="${condition}"></c:out>'>
<input type="submit" value="跳转" onclick="checkPage()">
</form>
</c:if>
</div>
</body>
</html>

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

上一篇:Node教程——Node+MongoDB案例实现用户信息的增删改查


下一篇:最简单的基于JSP标准标签库的增删改查