创立数据库表 examstudent,要求使用MVC模式实现下面的需求。
表结构如下:
DROP TABLE IF EXISTS examstudent
;
CREATE TABLE examstudent
(FlowID
int(11) NOT NULL AUTO_INCREMENT,Type
int(11) DEFAULT NULL,IDCard
varchar(18) DEFAULT NULL,ExamCard
varchar(15) DEFAULT NULL,StudentName
varchar(20) DEFAULT NULL,Location
varchar(20) DEFAULT NULL,Grade
int(11) DEFAULT NULL,
PRIMARY KEY (FlowID
)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO examstudent
VALUES ('4', '854524195263214584', '200523164754005', '王小红', '太原', '60');
1)在html页面以表格的方式显示所有的学生信息。页面上存在“添加学生”的链接
2)通过html页面插入一个新的student 信息
提示:信息录入成功!
3)在html页面中,输入身份证号或准考证号可以查询到学生的基本信息。
4)在html页面中,完成学生信息的删除功能,根据IDCard的删除
import java.sql.*;
import java.util.ArrayList;
public class ExamStudent {
private int flowId;
private int type;
private String iDCard;
private String examCard;
private String studentName;
private String location;
private int grade;
public ExamStudent() {
}
public ExamStudent(int flowId, int type, String iDCard, String examCard,
String studentName, String location, int grade) {
this.flowId = flowId;
this.type = type;
this.iDCard = iDCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
public ExamStudent(String iDCard, String examCard, String studentName,
String location, int grade) {
this.type = 0;
this.iDCard = iDCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
public int getFlowId() {
return flowId;
}
public void setFlowId(int flowId) {
this.flowId = flowId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getIDCard() {
return iDCard;
}
public void setIDCard(String iDCard) {
this.iDCard = iDCard;
}
public String getExamCard() {
return examCard;
}
public void setExamCard(String examCard) {
this.examCard = examCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
// 从ResultSet中提取学生信息并返回一个ExamStudent对象
private static ExamStudent extractData(ResultSet rs) throws SQLException {
ExamStudent student = new ExamStudent();
student.setFlowId(rs.getInt("FlowID"));
student.setType(rs.getInt("Type"));
student.setIDCard(rs.getString("IDCard"));
student.setExamCard(rs.getString("ExamCard"));
student.setStudentName(rs.getString("StudentName"));
student.setLocation(rs.getString("Location"));
student.setGrade(rs.getInt("Grade"));
return student;
}
// 获取所有学生的信息
public static ArrayList<ExamStudent> getAllStudents() throws SQLException {
ArrayList<ExamStudent> students = new ArrayList<ExamStudent>();
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from examstudent");
while (rs.next()) {
ExamStudent student = extractData(rs);
students.add(student);
}
rs.close();
stmt.close();
conn.close();
return students;
}
// 根据身份证号或准考证号查找学生的信息
public static ExamStudent findStudent(String id) throws SQLException {
ExamStudent student = null;
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from examstudent where " +
"IDCard = '" + id + "' or ExamCard = '" + id + "'");
if (rs.next()) {
student = extractData(rs);
}
rs.close();
stmt.close();
conn.close();
return student;
}
// 插入一个新的学生信息到数据库中
public boolean insert() throws SQLException {
if (type != 0) {
return false;
}
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
String sql = "insert into examstudent(Type, IDCard, ExamCard, StudentName, " +
"Location, Grade) values (0, '" + iDCard + "', '" + examCard +
"', '" + studentName + "', '" + location + "', " + grade + ")";
int row = stmt.executeUpdate(sql);
stmt.close();
conn.close();
return row == 1;
}
// 根据身份证号码删除学生信息
public static boolean delete(String id) throws SQLException {
Connection conn = DBUtil.getConnection();
Statement stmt = conn.createStatement();
String sql = "delete from examstudent where IDCard = '" + id + "'";
int row = stmt.executeUpdate(sql);
stmt.close();
conn.close();
return row == 1;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exam Students</title>
<style>
th, td {
padding: 5px;
text-align: center;
}
table {
border-collapse: collapse;
margin: 20px auto;
}
input[name="id"] {
margin-left: 20px;
}
input[type="submit"] {
margin-left: 20px;
}
</style>
</head>
<body>
<h1>Exam Students</h1>
<table border="1">
<tr>
<th>FlowID</th>
<th>Type</th>
<th>IDCard</th>
<th>ExamCard</th>
<th>StudentName</th>
<th>Location</th>
<th>Grade</th>
</tr>
<% ArrayList<ExamStudent> students = ExamStudent.getAllStudents(); %>
<% for (ExamStudent student : students) { %>
<tr>
<td><%= student.getFlowId() %></td>
<td><%= student.getType() %></td>
<td><%= student.getIDCard() %></td>
<td><%= student.getExamCard() %></td>
<td><%= student.getStudentName() %></td>
<td><%= student.getLocation() %></td>
<td><%= student.getGrade() %></td>
</tr>
<% } %>
</table>
<form action="addStudent.jsp">
<input type="submit" value="Add Student">
</form>
<form action="findStudent.jsp" method="post">
<label for="id">IDCard/ExamCard:</label>
<input type="text" name="id">
<input type="submit" value="Find">
</form>
<form action="deleteStudent.jsp" method="post">
<label for="id">IDCard:</label>
<input type="text" name="id">
<input type="submit" value="Delete">
</form>
</body>
</html>
addStudent.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Student</title>
</head>
<body>
<h1>Add Student</h1>
<%
if (request.getMethod().equalsIgnoreCase("post")) {
String idCard = request.getParameter("idCard");
String examCard = request.getParameter("examCard");
String studentName = request.getParameter("studentName");
String location = request.getParameter("location");
int grade = Integer.parseInt(request.getParameter("grade"));
ExamStudent student = new ExamStudent(idCard, examCard, studentName, location, grade);
boolean added = student.insert();
if (added) {
out.println("Information added successfully!");
} else {
out.println("Failed to add information!");
}
}
%>
<form action="addStudent.jsp" method="post">
<label for="idCard">IDCard:</label>
<input type="text" name="idCard"><br>
<label for="examCard">ExamCard:</label>
<input type="text" name="examCard"><br>
<label for="studentName">StudentName:</label>
<input type="text" name="studentName"><br>
<label for="location">Location:</label>
<input type="text" name="location"><br>
<label for="grade">Grade:</label>
<input type="text" name="grade"><br>
<input type="submit" value="Save">
</form>
<form action="index.jsp">
<input type="submit" value="Back">
</form>
</body>
</html>
findStudent.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Find Student</title>
</head>
<body>
<h1>Find Student</h1>
<%
if (request.getMethod().equalsIgnoreCase("post")) {
String id = request.getParameter("id");
ExamStudent student = ExamStudent.findStudent(id);
if (student != null) {
out.println("IDCard: " + student.getIDCard() + "<br>");
out.println("ExamCard: " + student.getExamCard() + "<br>");
out.println("StudentName: " + student.getStudentName() + "<br>");
out.println("Location: " + student.getLocation() + "<br>");
out.println("Grade: " + student.getGrade() + "<br>");
} else {
out.println("No such student!");
}
}
%>
<form action="findStudent.jsp" method="post">
<label for="id">IDCard/ExamCard:</label>
<input type="text" name="id">
<input type="submit" value="Find">
</form>
<form action="index.jsp">
<input type="submit" value="Back">
</form>
</body>
</html>
deleteStudent.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Delete Student</title>
</head>
<body>
<h1>Delete Student</h1>
<%
if (request.getMethod().equalsIgnoreCase("post")) {
String id = request.getParameter("id");
boolean deleted = ExamStudent.delete(id);
if (deleted) {
out.println("Information deleted successfully!");
} else {
out.println("Failed to delete information!");
}
}
%>
<form action="deleteStudent.jsp" method="post">
<label for="id">IDCard:</label>
<input type="text" name="id"><br>
<input type="submit" value="Delete">
</form>
<form action="index.jsp">
<input type="submit" value="Back">
</form>
</body>
</html>
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/testDB?useUnicode=true&characterEncoding=utf8";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}