您的位置:首页 > 其它

学生管理系统

2015-11-30 19:17 246 查看
数据库链接类

package Servlet;

import java.sql.*;
public class Dao {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Connection getconn() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root");
return conn;
}
public static void closeconn(Connection conn) throws SQLException {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}

}
方法类

package Servlet;

import java.sql.*;

public class GetDao {
public ResultSet GetAllStudents() throws SQLException {//显示数据
String sql = "select *from user";
Connection conn = null;
ResultSet rs = null;
conn = Dao.getconn();
if (conn != null) {
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
}
return rs;
}
public int SaveUser(String StuNo, String Name, String Sex,String Year,String JG,String Dept)//保存数据
throws SQLException {
String sql = "insert into user values(?,?,?,?,?,?)";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
ps.setString(2, Name);
ps.setString(3, Sex);
ps.setInt(4, (Integer.parseInt(Year)));
ps.setString(5, JG);
ps.setString(6, Dept);
i = ps.executeUpdate();
}
return i;
}
public ResultSet FineOne(String StuNo) throws SQLException {//查找数据
String sql = "select *from user where StuNo=?";
Connection conn = null;
ResultSet rs = null;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
rs = ps.executeQuery();
}
return rs;
}

public int UpDate(String StuNo, String Name, String Sex,String Year,String JG,String Dept)//插入数据
throws SQLException {
String sql = "update user set Name=?,Sex=?,Year=?,JG=?,Dept=? where StuNo=?";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, Name);
ps.setString(2, Sex);
ps.setInt(3, (Integer.parseInt(Year)));
ps.setString(4, JG);
ps.setString(5, Dept);
ps.setString(6, StuNo);
i = ps.executeUpdate();
}
return i;
}

public int DeleteOne(String StuNo) throws SQLException {//删除方法
String sql = "delete from user where StuNo=?";
Connection conn = null;
int i = 0;
conn = Dao.getconn();
if (conn != null) {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, StuNo);
i = ps.executeUpdate();
}
return i;
}
}
显示页面

<%@ page language="java" import="java.sql.*,Servlet.*"
pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=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>用户信息</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">
</head>
<%
GetDao dao = new GetDao();
ResultSet rs = dao.GetAllStudents();
%>
<body>
<center>
<h1>学籍管理</h1>
<p>
<a href="Students.jsp">刷新</a>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>系别</th>
<th>修改</th>
<th>删除</th>
</tr>
<%
if (rs != null) {
while (rs.next()) {
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getInt(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><a href="Change.jsp?StuNo=<%=rs.getString(1)%>">修改</a>
<!--
</td>
<td><a href="Delete.jsp?StuNo=<%=rs.getString(1)%>">删除</a>
</td>
-->
<td><a href="Delete.jsp?StuNo=<%=rs.getString(1)%>"
onclick="alert('确定删除?')">删除</a>
</td><!-- 刪除提示 -->
</tr>
<%
}
rs.close();
}
%>

</table>
<a href="AddUser.jsp">添加用户</a>
</center>
</body>
</html>
添加用户

<%@ 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>添加用户</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">
</head>
<body>
<center>
<form action="SaveUser.jsp" method="post">
<h1>添加用户</h1>
<table>
<tr>
<td>学号:</td>
<td><input type="text" name="StuNo"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>性别:</td>
<td><select name="Sex" size="1">
<option value="Man">男</option>
<option value="Woman">女</option>
</select></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="Year"></td>
</tr>
<tr>
<td>籍贯:</td>
<td><input type="text" name="JG"></td>
</tr>
<tr>
<td>系别:</td>
<td><input type="text" name="Dept"></td>
</tr>
</table>
<input type="submit" value="注册"> <input type="reset"
value="取消">
</form>
</center>
</body>
</html>
jsp保存页面

<%@ page language="java" import="java.sql.*,Servlet.*" pageEncoding="UTF-8"%>

<%@ page contentType="text/html; charset=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>插入数据</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">

</head>

<%
GetDao dao = new GetDao();
request.setCharacterEncoding("UTF-8");//中文读取处理
String StuNo = request.getParameter("StuNo");
String Name = request.getParameter("Name");
String Sex = request.getParameter("Sex");
String Year = request.getParameter("Year");
String JG = request.getParameter("JG");
String Dept = request.getParameter("Dept");
int i = dao.SaveUser(StuNo, Name, Sex, Year, JG, Dept);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("添加失败");
}
%>
<body>
</body>
</html>
修改页面

<%@ page language="java" import="java.sql.*,Servlet.*"
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>修改信息</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">

</head>
<%
GetDao dao = new GetDao();
String StuNo = request.getParameter("StuNo");
ResultSet rs = dao.FineOne(StuNo);
%>
<body>
<center>
<form action="Save.jsp" method="post">
<h1>信息修改</h1>
<%
if (rs != null) {
while (rs.next()) {
%>
<table border="0">
<tr>
<td>学号:</td>
<td><input type="text" name="StuNo"
value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="Name"
value="<%=rs.getString(2)%>"></td>
</tr>
<tr>
<td>性别:</td>
<td><select name="Sex" size="1">
<%
String s = rs.getString(3);
if (s.equals("Man")) {
%>
<option value="Man">男</option>
<option value="Woman">女</option>
<%
} else {
%>
<option value="Woman">女</option>
<option value="Man">男</option>
<%
}
%>
</select></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="Year" value="<%=rs.getInt(4)%>">
</td>
</tr>
<tr>
<td>籍贯:</td>
<td><input type="text" name="JG" value="<%=rs.getString(5)%>">
</td>
</tr>
<tr>
<td>系别:</td>
<td><input type="text" name="Dept"
value="<%=rs.getString(6)%>"></td>
</tr>
<%
}
rs.close();
}
%>
</table>
<input type="submit" name="Submit" value="提交"> <input
type="reset" name="Cancel" value="取消">
</form>
</center>
</body>
</html>


修改后保存

<%@ page language="java" import="java.sql.*,Servlet.*"
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>保存数据</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">
</head>
<%
GetDao dao = new GetDao();
request.setCharacterEncoding("UTF-8");//中文读取处理
String StuNo = request.getParameter("StuNo");
String Name = request.getParameter("Name");
String Sex = request.getParameter("Sex");
String Year = request.getParameter("Year");
String JG = request.getParameter("JG");
String Dept = request.getParameter("Dept");
int i = dao.UpDate(StuNo, Name, Sex, Year, JG, Dept);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("存入失败!");
}
%>
<body>

</body>
</html>
删除页面

<%@ page language="java" import="java.sql.*,Servlet.*"
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>Delete</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">

</head>

<%
GetDao dao = new GetDao();
String StuNo = request.getParameter("StuNo");
int i = dao.DeleteOne(StuNo);
if (i > 0) {
response.sendRedirect("Students.jsp");
} else {
out.print("删除失败!");
}
%>
<body>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: