control
1、AddStudentServlet.java
doGet
StudentDao studentDao = new StudentDaoImpl();
//1.接受表单提交信息
String name=request.getParameter("name");
int sno=Integer.parseInt(request.getParameter("sno"));
String sclass=request.getParameter("sclass");
String dpSno=request.getParameter("dpSno");
//操作类型(添加/删除)
String type=request.getParameter("type");
//2.创建学生对象
Student stu=new Student();
if("add".equals(type)) {
//将表单信息赋值给学生对象
stu.setsName(name);
stu.setSno(sno);
stu.setDpSno(dpSno);
stu.setSclass(sclass);
// 向Student表插入一个用户信息
boolean b=studentDao.insert(stu);
if(b) {
request.setAttribute("message", "插入成功!");
}else {
request.setAttribute("message", "插入失败!");
}
}else {
stu.setsName(name);
stu.setSno(sno);
stu.setDpSno(dpSno);
stu.setSclass(sclass);
studentDao.update(stu);
}
ArrayList<Student> list=new ArrayList<Student>();
list=studentDao.findAllStudent();
request.setAttribute("list", list);
request.getRequestDispatcher("/showStudentInfo.jsp").forward(request,response);
}
2、DeleteStudentServlet.java
doPost
int sno=Integer.parseInt(request.getParameter("sno"));
// 删除一个学生
StudentDao studentDao = new StudentDaoImpl();
boolean b=studentDao.deleteStudentBySno(sno);
if(b) {
request.setAttribute("message", "删除成功!");
}else {
request.setAttribute("message", "删除失败!");
}
ArrayList<Student> list=new ArrayList<Student>();
list=studentDao.findAllStudent();
request.setAttribute("list", list);
request.getRequestDispatcher("/showStudentInfo.jsp").forward(request,response);
3、exitServlet.java
doGet
HttpSession session=request.getSession();
if(session.getAttribute("s")!=null) {
session.removeAttribute("s");
}
request.getRequestDispatcher("/login.jsp").forward(request, response);
4、LoginFilter.java
doFilter
//转换request和response对象
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse res=(HttpServletResponse) response;
//创建session对象
HttpSession session=req.getSession();
//获取学生信息
Student s=(Student)session.getAttribute("s");
//判断学生是否登录
if(s==null) {//没有登录
req.getRequestDispatcher("/login.jsp").forward(request, response);
}else {//放行
chain.doFilter(request, response);
}
5、LoginServlet.java
doGet
//1、获取表单信息
int sno=Integer.parseInt(request.getParameter("sno"));
//2、创建数据库访问层接口对象
StudentDao dao=new StudentDaoImpl();
//3、根据学号查询学生
Student s=dao.findStudentBySno(sno);
if(s!=null) {//学生存在
//4、创建session对象
HttpSession session=request.getSession();
//5、将学生放入到session对象
session.setAttribute("s",s);
//请求并转发
request.getRequestDispatcher("/SelectStudentServlet").forward(request,response);
}else {//学生不存在
//存储错误
request.setAttribute("errorMessage", "该学生不存在!");
//请求并转发登录页面
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
6、SelectStudentServlet.java
doGet
StudentDao studentDao = new StudentDaoImpl();
ArrayList<Student> list=new ArrayList<Student>();
list=studentDao.findAllStudent();
request.setAttribute("list", list);
request.getRequestDispatcher("/showStudentInfo.jsp").forward(request,response);
7、UpdateStudentServlet.java
doPost
StudentDao studentDao = new StudentDaoImpl();
int sno=Integer.parseInt(request.getParameter("sno"));
Student stu=studentDao.findStudentBySno(sno);
request.setAttribute("stu", stu);
request.getRequestDispatcher("/updateStudent.jsp").forward(request,response);
dao
1、StudentDao.java
/**
* 数据库接口
* @author Administrator
*
*/
public interface StudentDao {
/**
* 插入一个学生
* @param student
* @return
*/
public boolean insert(Student student);
/**
* 查询所有学生数据
* @return 学生集合
*/
public ArrayList<Student> findAllStudent();
/**
* 根据学号获取学生信息
* @param sno 学号
* @return 查询学生对象
*/
public Student findStudentBySno(int sno);
/**
* 根据学号删除学生
* @param sno 学号
* @return true/false
*/
public boolean deleteStudentBySno(int sno);
/**
* 更新一个学生信息(根据学号sno)
* @param student
* @return true/false
*/
public boolean update(Student student);
2、StudentDaoImpl.java
public class StudentDaoImpl implements StudentDao {
/**
* 插入一个学生
* @param student
* @return
*/
public boolean insert(Student student) {
try {
String sql = "insert into student(sName,sno,dpSno,sclass) "+
"values('"
+ student.getsName()
+ "',"
+ student.getSno()
+ ",'"
+ student.getDpSno()
+ "','"
+ student.getSclass()
+ "')";
boolean flag = JDBCUtils.executeUpdate(sql);
return flag;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 查询所有学生数据
* @return
*/
public ArrayList<Student> findAllStudent() {
//定义学生集合
ArrayList<Student> list = new ArrayList<Student>();
try {
String sql = "select * from student";
ResultSet rs = JDBCUtils.executeQuery(sql);
//循环遍历结果集并放入学生集合中
while (rs.next()) {
Student student = new Student();
student.setsName(rs.getString("sName"));
student.setSno(rs.getInt("sno"));
student.setDpSno(rs.getString("dpSno"));
student.setSclass(rs.getString("sclass"));
list.add(student);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 根据学号查询某个学生(前题学号是唯一)
public Student findStudentBySno(int sno) {
try {
String sql = "select * from student where sno=" + sno;
ResultSet rs = JDBCUtils.executeQuery(sql);
while (rs.next()) {
Student student = new Student();
student.setsName(rs.getString("sName"));
student.setSno(rs.getInt("sno"));
student.setDpSno(rs.getString("dpSno"));
student.setSclass(rs.getString("sclass"));
return student;
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据学号删除学生
* @param sno 学号
* @return true/false
*/
public boolean deleteStudentBySno(int sno) {
try {
String sql = "delete from student where sno=" + sno;
boolean flag = JDBCUtils.executeUpdate(sql);
return flag;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 更新一个学生信息(根据学号sno)
* @param student
* @return true/false
*/
public boolean update(Student student) {
try {
String sql = "update student set sName='" + student.getsName()
+ "',dpSno='"+student.getDpSno()+"',sclass='"+ student.getSclass() + "' where sno=" + student.getSno();
boolean flag = JDBCUtils.executeUpdate(sql);
return flag;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
Entity
1、Student.java
/**
* 学生实体类
* @author Administrator
*
*/
public class Student {
private String sName;
private int sno;
private String dpSno;
private String sclass;
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getDpSno() {
return dpSno;
}
public void setDpSno(String dpSno) {
this.dpSno = dpSno;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
}
jdbc
1、JDBCUtils.java
/**
* 数据库连接工具包
* @author Administrator
*
*/
public class JDBCUtils {
static Connection conn =null;
static Statement stmt=null;
static ResultSet rs=null;
/**
* 加载驱动
* @return 数据库连接对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/st?useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username,
password);
return conn;
}
/**
* 释放数据库连接资源
*/
public static void release() {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
/**
* 执行查询SQL语句,并返回结果集
* @param sql
* @return
*/
public static ResultSet executeQuery(String sql){
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 执行查询SQL语句,并返回是否成功
* @param sql
* @return
*/
public static boolean executeUpdate(String sql){
int num=0;
try {
conn = getConnection();
stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(num>0) {
return true;
}else {
return false;
}
}
}
jsp文件
1、login.jsp
<body>
<h1>${errorMessage }</h1>
<form action="loginServlet" method="get" name="adminMsg">
学号:<input type="text" name="sno"/><br/><br/>
<input type="submit" value="登录" />
</form>
</body>
2、showStudentInfo.jsp
<body>
<h1>学生列表</h1>
<table border="1">
<tr>
<th width="100px;">姓名</th>
<th width="100px;">学号</th>
<th width="100px;">班级</th>
<th width="100px;">系别</th>
<th width="100px;">操作</th>
</tr>
<c:if test="${list!=null }">
<c:forEach items="${list}" var="s">
<tr align="center">
<td>${s.sName}</td>
<td>${s.sno}</td>
<td>${s.sclass}</td>
<td>${s.dpSno}</td>
<td><a href="updateStudentServlet?sno=${s.sno}">修改</a> <a href="deleteStudentServlet?sno=${s.sno}">删除</a></td>
</tr>
</c:forEach>
</c:if>
</table><br/>
<a href="studentAdd.jsp">返回继续添加</a> <a href="exitServlet">退出登录</a>
</body>
3、studentAdd.jsp
<body>
<h1>学生注册</h1>
<form action="addStudentServlet" method="get" name="stuInfo">
<input name="type" type="hidden" value="add"/>
学生姓名:<input type="text" name="name"/><br/><br/>
学号:<input type="text" name="sno"/><br/><br/>
班级:<select name="sclass">
<option value="计本1801班">计本1801班</option>
<option value="计本1802班">计本1802班</option>
<option value="计本1803班">计本1803班</option>
</select><br/><br/>
系别:<select name="dpSno">
<option value="C10001">计算机系</option>
<option value="E10001">英语系</option>
<option value="M10001">数学系</option>
</select><br/><br/>
<input type="submit" value="注册"/>
</form>
</body>
4、updateStudent.jsp
<body>
<h1>学生注册</h1>
<form action="addStudentServlet" method="get" name="stuInfo">
<input name="type" type="hidden" value="update"/>
学生姓名:<input type="text" name="name" value="${stu.sName }"/><br/><br/>
学号:<input type="text" name="sno" value="${stu.sno }"/><br/><br/>
班级:<select name="sclass">
<option value="计本1801班" <c:if test="${stu.sclass=='计本1801班'}">selected</c:if>>计本1801班</option>
<option value="计本1802班" <c:if test="${stu.sclass=='计本1802班'}">selected</c:if>>计本1802班</option>
<option value="计本1803班" <c:if test="${stu.sclass=='计本1803班'}">selected</c:if>>计本1803班</option>
</select><br/><br/>
系别:<select name="dpSno">
<option value="C10001"<c:if test="${stu.dpSno=='C10001'}">selected</c:if>>计算机系</option>
<option value="E10001"<c:if test="${stu.dpSno=='E10001'}">selected</c:if>>英语系</option>
<option value="M10001"<c:if test="${stu.dpSno=='M10001'}">selected</c:if>>数学系</option>
</select><br/><br/>
<input type="submit" value="修改"/>
</form>
</body>