javaweb综合训练
1.创建表t_student,其中包含学号stuno,姓名stuname,性别stusex三个字段,在表中插入一些测试数据。
2.实现下述功能:
- 编写显示所有学生资料信息页面,其中包含一个表单用来根据学生姓名模糊查询匹配的学生信息。
- 在学生信息后面增加一个“删除学生信息”链接,单击链接可以将学生信息从数据库中删除,然后跳转到显示所有学生资料信息页面
- 部分代码:
package com.lab.model;
public class Student {
private String stuno;
private String stuname;
private String stusex;
public Student() {
}
public Student(String stuno, String stuname, String stusex) {
this.stuno = stuno;
this.stuname = stuname;
this.stusex = stusex;
}
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
@Override
public String toString() {
return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stusex=" + stusex + "]";
}
}
package com.lab.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.lab.model.Student;
import com.lab.util.DatabaseBean;
public class StudentDao {
public List<Student> queryAllStudents() {
List<Student> students = new ArrayList<>();
String sql = "select * from t_student";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery()) {
while (rs.next()) {
Student stu = new Student();
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
students.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public List<Student> queryAllStudents(String stuname) {
List<Student> students = new ArrayList<>();
String sql = "select * from t_student where stuname like?";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql)) {
psmt.setString(1, "%" + stuname + "%");
try (ResultSet rs = psmt.executeQuery()) {
while (rs.next()) {
Student stu = new Student();
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
students.add(stu);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
public boolean deleteStudent(String stuno) {
String sql = "delete from t_student where stuno=?";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql)) {
psmt.setString(1, stuno);
int result = psmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean addStudent(Student stu) {
String sql = "insert into t_student values(?,?,?)";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql)) {
psmt.setString(1, stu.getStuno());
psmt.setString(2, stu.getStuname());
psmt.setString(3, stu.getStusex());
int result = psmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean updateStudent(Student stu) {
String sql = "update t_student set stuname=?,stusex=? where stuno=?";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql)) {
psmt.setString(1, stu.getStuname());
psmt.setString(2, stu.getStusex());
psmt.setString(3, stu.getStuno());
int result = psmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public Student queryStudent(String stuno) {
Student stu = null;
String sql = "select * from t_student where stuno=?";
try (Connection conn = DatabaseBean.getConnection();
PreparedStatement psmt = conn.prepareStatement(sql)) {
psmt.setString(1, stuno);
try (ResultSet rs = psmt.executeQuery()) {
if (rs.next()) {
stu = new Student();
stu.setStuno(rs.getString("stuno"));
stu.setStuname(rs.getString("stuname"));
stu.setStusex(rs.getString("stusex"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return stu;
}
}
package com.lab.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseBean {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String path = "C:/Users/李心洁/Documents/Database6.accdb";
String url = "jdbc:ucanaccess://" + path;
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs, PreparedStatement psmt, Connection conn) {
try {
if (rs!= null) {
rs.close();
}
if (psmt!= null) {
psmt.close();
}
if (conn!= null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.lab.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
public class SuperAciton extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {
private static final long serialVersionUID = 1L;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
protected ServletContext application;
@Override
public void setServletContext(ServletContext application) {
this.application = application;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
}
package com.lab.action;
import java.util.List;
import com.lab.dao.StudentDao;
import com.lab.model.Student;
public class StudentAction extends SuperAciton {
private StudentDao studentDao = new StudentDao();
public String find() {
List<Student> students = studentDao.queryAllStudents();
request.setAttribute("students", students);
return "find_success";
}
public String query() {
String stuname = request.getParameter("stuname");
List<Student> students = studentDao.queryAllStudents(stuname);
request.setAttribute("students", students);
return "query_success";
}
public String delete() {
String stuno = request.getParameter("stuno");
studentDao.deleteStudent(stuno);
return "delete_success";
}
public String add() {
String stuno = request.getParameter("stuno");
String stuname = request.getParameter("stuname");
String stusex = request.getParameter("stusex");
Student stu = new Student(stuno, stuname, stusex);
boolean flag = studentDao.addStudent(stu);
if (flag) {
return "add_success";
} else {
return "add_failure";
}
}
public String update() {
String stuno = request.getParameter("stuno");
Student student = studentDao.queryStudent(stuno);
request.setAttribute("student", student);
return "update_success";
}
public String save() {
String stuno = request.getParameter("stuno");
String stuname = request.getParameter("stuname");
String stusex = request.getParameter("stusex");
Student stu = new Student(stuno, stuname, stusex);
boolean flag = studentDao.updateStudent(stu);
if (flag) {
return "save_success";
} else {
return "save_failure";
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页面</title>
</head>
<body>
首页面
<a href="find.action">查询所有学生数据</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有学生数据</title>
</head>
<body>
<form action="query.action" method="get">
请输入要查询的学生姓名(支持模糊查询) <input type="text" name="stuname"
placeholder="学生姓名"> <input type="submit" value="查询">
</form>
<table>
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>操作</td>
</tr>
<c:forEach items="${students }" var="stu">
<tr>
<td>${stu.stuno }</td>
<td>${stu.stuname }</td>
<td>${stu.stusex }</td>
<td><a href="delete.action?stuno=${stu.stuno }">删除</a> <a
href="update.action?stuno=${stu.stuno }">修改</a></td>
</tr>
</c:forEach>
</table>
<a href="student_add.jsp">添加学生数据</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生数据</title>
</head>
<body>
<form action="add.action" method="post">
学号: <input type="text" name="stuno" required><br>
姓名: <input type="text" name="stuname" required><br>
性别: <input type="text" name="stusex" required><br>
<input type="submit" value="添加">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改学生数据</title>
</head>
<body>
<form action="save.action" method="post">
<input type="hidden" name="stuno" value="${student.stuno }">
姓名: <input type="text" name="stuname" value="${student.stuname }" required><br>
性别: <input type="text" name="stusex" value="${student.stusex }" required><br>
<input type="submit" value="保存">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result>index.jsp</result>
</action>
<action name="*" class="com.lab.action.StudentAction" method="{1}">
<result name="find_success">student_find.jsp</result>
<result name="query_success">student_find.jsp</result>
<result name="delete_success" type="redirectAction">find</result>
<result name="add_success" type="redirectAction">find</result>
<result name="add_failure" type="redirect">student_add.jsp</result>
<result name="update_success">student_update.jsp</result>
<result name="save_success" type="redirectAction">find</result>
<result name="save_failure" type="chain">update</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>StudentManagement</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</