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錯誤,刪除功能還未寫頁面。