1.学生类
package com.bean.Impl;
import com.bean.Interface.*;;
public class Student implements StudentItf{
private String stuName = null;
private String stuNum = null;
private String stuClass = null;
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
}
2.操作系统连接类
package com.bean.sql;
import java.sql.*;
public class SqlCon {
private Connection con = null;
public Connection getCon(){
String url = "jdbc:mysql://localhost:3306/studentSystem";
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("加载驱动失败");
}
String username = "chenjunxian";
String password = "123456";
try {
con = DriverManager.getConnection(url,username,password);
System.out.println("数据库验证成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库验证失败");
}
return con;
}
}
3.数据库操作类
package com.bean.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.bean.sql.SqlCon;
public class SqlOper implements com.bean.Interface.SqlOper {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private Statement stmt = null;
public void close() {
// TODO Auto-generated method stub
try {
if(ps!=null)
{
ps.close();
ps = null;
}
if(rs!=null)
{
rs.close();
rs = null;
}
if(stmt!=null)
{
stmt.close();
stmt = null;
}
if(con!=null)
{
con.close();
con = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭失败");
}
}
public ArrayList<Student> nameSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuName = stu.getStuName();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuName = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuName);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
stuList.add(reStu);
}
System.out.println("使用名字查询成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用名字查询失败");
}
close();
return stuList;
}
public ArrayList<Student> classSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuClass = stu.getStuClass();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuClass = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuClass);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
stuList.add(reStu);
reStu = null;
}
System.out.println("使用班级查询成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用班级查询失败");
}
close();
return stuList;
}
public ArrayList<Student> numSelect(Student stu) {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
String stuNum = stu.getStuNum();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuNum = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuNum);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
System.out.println(rs.getString("stuName"));
stuList.add(reStu);
}
System.out.println("使用学号查询成功");
System.out.println("sqloper"+stuList.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("使用学号查询失败");
}
close();
return stuList;
}
public boolean stuInsert(Student stu) {
// TODO Auto-generated method stub
boolean isInsert = false;
this.con = new SqlCon().getCon();
String sql = "insert into student (stuName,stuNum,stuClass) values (?,?,?)";
try {
ps = this.con.prepareStatement(sql);
ps.setString(1, stu.getStuName());
ps.setString(2, stu.getStuNum());
ps.setString(3, stu.getStuClass());
ps.executeUpdate();
System.out.println("执行插入语句成功");
isInsert = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行插入语句失败");
}
close();
return isInsert;
}
public boolean stuDrop(Student stu) {
// TODO Auto-generated method stub
boolean isInsert = false;
this.con = new SqlCon().getCon();
String sql = "delete from student where stuNum = ?";
try {
ps = this.con.prepareStatement(sql);
ps.setString(1, stu.getStuNum());
ps.executeUpdate();
System.out.println("执行删除语句成功");
isInsert = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行删除语句失败");
}
close();
return isInsert;
}
public boolean stuUpdata(Student stu) {
// TODO Auto-generated method stub
boolean isInsert = false;
this.con = new SqlCon().getCon();
String sql = "update student set stuName = ?,stuNum = ?,stuClass = ? where stuNum = ?";
try {
ps = this.con.prepareStatement(sql);
ps.setString(1, stu.getStuName());
ps.setString(2, stu.getStuNum());
ps.setString(3, stu.getStuClass());
ps.setString(4, stu.getStuNum());
ps.executeUpdate();
System.out.println("执行插入语句成功");
isInsert = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("执行插入语句失败");
}
close();
return isInsert;
}
public void cloce() {
// TODO Auto-generated method stub
}
public ArrayList<Student> Select() {
// TODO Auto-generated method stub
ArrayList<Student> stuList = new ArrayList<Student>();
Student reStu = null;
this.con = new SqlCon().getCon();
String sql = "select * from student";
try {
this.ps = con.prepareStatement(sql);
this.rs = ps.executeQuery();
while(rs.next())
{
reStu = new Student();
reStu.setStuName(rs.getString("stuName"));
reStu.setStuNum(rs.getString("stuNum"));
reStu.setStuClass(rs.getString("stuClass"));
stuList.add(reStu);
reStu=null;
}
System.out.println("查询全部成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("查询全部失败");
}
close();
return stuList;
}
public boolean testStudent(Student stu) {
// TODO Auto-generated method stub
boolean isExist = false;
String stuNum = stu.getStuNum();
this.con = new SqlCon().getCon();
String sql = "select * from student where stuNum = ?";
try {
this.ps = con.prepareStatement(sql);
this.ps.setString(1, stuNum);
this.rs = ps.executeQuery();
while(rs.next())
{
isExist = true;
}
System.out.println("用户存在");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("查询失败");
}
close();
return isExist;
}
}
4.处理页面请求的servlet类
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.Impl.SqlOper;
import com.bean.Impl.Student;
public class stuServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public stuServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doDelete method of the servlet. <br>
*
* This method is called when a HTTP delete request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//防止中文乱码所进行的编码设置
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//使用SqlOper
ArrayList<Student> stuList = null;
SqlOper sqloper = new SqlOper();
Student stu = null;
String optype = (String)request.getParameter("optype");
if(optype.equals("search"))
{
//读取查询类型和查询值
String searchtype=request.getParameter("searchtype");
String searchvalue=request.getParameter("searchvalue");
System.out.println(searchtype);
System.out.println(searchvalue);
//如果按照学生姓名查询
if(searchtype.equals("stuName"))
{
stuList = new ArrayList<Student>();
stu = new Student();
stu.setStuName(searchvalue);
stuList = sqloper.nameSelect(stu);
request.setAttribute("searchResult", stuList);
request.getRequestDispatcher("showSearchResult.jsp").forward(request, response);
stuList = null;
stu = null;
}
else if(searchtype.equals("stuNum"))
{
stuList = new ArrayList<Student>();
stu = new Student();
stu.setStuNum(searchvalue);
stuList = sqloper.numSelect(stu);
System.out.println("stuServlet"+stuList.toString());
request.setAttribute("searchResult", stuList);
request.getRequestDispatcher("showSearchResult.jsp").forward(request, response);
stuList = null;
stu = null;
}
else if(searchtype.equals("stuClass"))
{
stuList = new ArrayList<Student>();
stu = new Student();
stu.setStuClass(searchvalue);
stuList = sqloper.classSelect(stu);
request.setAttribute("searchResult", stuList);
request.getRequestDispatcher("showSearchResult.jsp").forward(request, response);
stuList = null;
stu = null;
}
}
if(optype.equals("add"))
{
String stuName = request.getParameter("stuName");
String stuNum = request.getParameter("stuNum");
String stuClass = request.getParameter("stuClass");
stu = new Student();
stu.setStuName(stuName);
stu.setStuNum(stuNum);
stu.setStuClass(stuClass);
String isInsert = null;
if(sqloper.stuInsert(stu))
{
isInsert = "添加成功";
request.setAttribute("isInsert", isInsert);
//request.getRequestDispatcher("addSucc.jsp").forward(request, response);
}else
{
request.getRequestDispatcher("addFail.jsp").forward(request, response);
}
request.getRequestDispatcher("addStudent.jsp").forward(request, response);
stu = null;
}
if(optype.equals("test"))
{
String stuNum=request.getParameter("stuNum");
stu = new Student();
stu.setStuNum(stuNum);
boolean isExist = sqloper.testStudent(stu);
if(isExist)
{
stu = sqloper.numSelect(stu).get(0);
String suboptype = request.getParameter("suboptype");
if(suboptype.equals("stuUpdate"))
{
request.setAttribute("searchResult", stu);
request.getRequestDispatcher("updateStudent2.jsp").forward(request, response);
}
else if(suboptype.equals("stuDrop"))
{
request.setAttribute("searchResult", stu);
request.getRequestDispatcher("deleteStu2.jsp").forward(request, response);
}
stu = null;
}
else
{
request.getRequestDispatcher("updateFail.jsp").forward(request, response);
}
}
if(optype.equals("updateStudent"))
{
stu = new Student();
stu.setStuNum(request.getParameter("stuNum"));
stu.setStuName(request.getParameter("stuName"));
stu.setStuClass(request.getParameter("stuClass"));
if(sqloper.stuUpdata(stu))
{
String updateSucc = "修改成功";
request.getRequestDispatcher("updateStudent2.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("updateFail.jsp").forward(request, response);
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
5.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>学生管理</h1>
<h2>
<a href="showStudent.jsp">显示学生表</a>
<a href="serchStudent.jsp">查找学生</a>
<a href="addStudent.jsp">添加学生</a>
<a href="updateStudent.jsp">修改学生信息</a>
<a href="deleteStudent.jsp">删除学生</a>
</h2>
</body>
</html>
2.showStudent.jsp
<%@ page language="java" import="java.util.*,com.bean.Impl.*,com.bean.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showStudent.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div style = "width:*90px;margin:0,auto;">
<%
SqlOper sqloper = new SqlOper();
ArrayList<Student> stuList = sqloper.Select();
%>
<center>
所有学生列表<hr/>
<input type = "button" value="返回操作页面" class = "buttontype" onclick="location.href='index.jsp'"><br>
<!-- 用表格来设计主页面 -->
<table width=90% border=1>
<tr>
<td width=10%>学号</td>
<td width=10%>姓名</td>
<td width=10%>班级</td>
</tr>
<!-- 显示从数据表中读取的信息 -->
<%
Student stu;
for(int i = 0;i<stuList.size();i++)
{
stu = stuList.get(i);
%>
<tr>
<td><%=stu.getStuNum() %></td>
<td><%=stu.getStuName() %></td>
<td><%=stu.getStuClass() %></td>
</tr>
<%
}
%>
</table>
<input type ="button" value = "返回操作主页面" class="buttontype" onclick="location.href='index.jsp'"/><br>
</center>
</div>
</body>
</html>
6.showSearchResult.jsp
<%@ page language="java" import="java.util.*,com.bean.Impl.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showSearchResult.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div style = "width:*90px;margin:0,auto;">
<%
ArrayList<Student> stuList = (ArrayList<Student>)request.getAttribute("searchResult");
%>
<center>
所有学生列表<hr/>
<input type = "button" value="返回操作页面" class = "buttontype" onclick="location.href='index.jsp'"><br>
<!-- 用表格来设计主页面 -->
<table width=90% border=1>
<tr>
<td width=10%>学号</td>
<td width=10%>姓名</td>
<td width=10%>班级</td>
</tr>
<!-- 显示从数据表中读取的信息 -->
<%
Student stu;
for(int i = 0;i<stuList.size();i++)
{
stu = stuList.get(i);
%>
<tr>
<td><%=stu.getStuNum() %></td>
<td><%=stu.getStuName() %></td>
<td><%=stu.getStuClass() %></td>
</tr>
<%
}
%>
</table>
<input type ="button" value = "返回操作主页面" class="buttontype" onclick="location.href='index.jsp'"/><br>
</center>
</div>
</body>
</html>
7.addStudent.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addStudent.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
添加学生<hr>
<form method=post action="stuServlet?optype=add">
学号<input type="text" name="stuNum" value="" class="two"><br>
姓名<input type="text" name="stuName" value="" class="two"><br>
班级<input type="text" name="stuClass" value="" class="two"><br>
<input type="submit" class="buttontype" value="提交"/>
<input type="reset" class="buttontype" value="重置"/>
</form>
<input type="button" value="返回操作主页面" class = "buttontype" onclick="location.href='index.jsp'"/><br/>
提交状态<input type="text" value="<%=request.getAttribute("isInsert") %>" >
</body>
</html>
8.searchStudent.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'serchStudent.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" language="javaScript">
function getRadioValue(){
var aa = document.getElementByName("select");
var bb = document.getElementById("searchvalue");
location.href='index.jsp';
for(var i = 0;i<aa.length;i++)
{
if(aa[i].checked)
{
location.href="stuServlet?optype=search&searchtype="+aa[i].value+"&searchvalue="+bb.value;
}
}
}
</script>
</head>
<body>
查找学生<hr>
<form method=post action="stuServlet?optype=search">
<input type = "radio" name="searchtype" value="stuNum" class="two" checked="checked">学号<br>
<input type = "radio" name="searchtype" value="stuName" class="two" checked="checked">姓名<br>
<input type = "radio" name="searchtype" value="stuClass" class="two" checked="checked">班级<br>
请输入查找值:<input type="text" class="two" name="searchvalue" size=100><br>
<input type="submit" class="buttontype" value="提交"/>
<input type="reset" class="buttontype" value="重置"/>
</form>
<input type="button" value="返回操作主页面" class="buttontype" onclick="location.href='index.jsp'"/><br>
</body>
</html>
9.upadteStudent.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'updateStudent.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
修改学生信息<hr>
<form action="stuServlet?optype=test&suboptype=stuUpdate" method=post>
请输入学号,点击"提交"按钮查看有无要删除的商品。<br><br>
学号<input type="text" name="stuNum" value="" class="two"><br>
<input type="submit" class="buttontype" value="提交"/>
<input type="reset" class="buttontype" value="重置"/>
</form>
<input type="button" value="返回操作主页面" class="buttontype" onclick="localtion.href='index.jsp'"/><br>
</body>
</html>
10.updateStudent2.jsp
<%@ page language="java" import="java.util.* ,com.bean.Impl.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'updateStudent2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 修改学生信息<hr> <% Student stu = (Student)request.getAttribute("searchResult"); %> <form method=post action="stuServlet?optype=updateStudent"> 学号<input type="text" name="stuNum" value="<%=stu.getStuNum() %>" class="two"><br> 姓名<input type="text" name="stuName" value="<%=stu.getStuName() %>" class="two"><br> 班级<input type="text" name="stuClass" value="<%=stu.getStuClass() %>" class="two"><br> <input type="submit" class="buttontype" value="提交" /> <input type="reset" class="buttontype" value="重置"/> </form> <input type="button" value="返回操作主页面" class="buttontype" onclick="location.href='index.jsp'"/><br> </body> </html>
由于少写了删除还有更新后的跳转页面,所以更新功能还不完全,会报505错误,删除功能还未写页面。