JSP设计一个简单的学生成绩管理系统
时间: 2023-07-14 08:12:34 浏览: 109
学生成绩管理系统可以分为三个部分:前端界面、后端数据处理和数据库存储。下面是一个简单的JSP实现:
1. 前端界面
```html
<!-- index.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生成绩管理系统</title>
</head>
<body>
<h1>学生成绩管理系统</h1>
<form action="search.jsp" method="post">
<label>请输入学生姓名:</label>
<input type="text" name="name">
<input type="submit" value="查询">
</form>
<a href="add.jsp">添加学生信息</a>
</body>
</html>
```
```html
<!-- add.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
</head>
<body>
<h1>添加学生信息</h1>
<form action="addStudent.jsp" method="post">
<label>姓名:</label>
<input type="text" name="name"><br><br>
<label>性别:</label>
<input type="radio" name="gender" value="男" checked>男
<input type="radio" name="gender" value="女">女<br><br>
<label>年龄:</label>
<input type="number" name="age"><br><br>
<label>语文成绩:</label>
<input type="number" name="chinese"><br><br>
<label>数学成绩:</label>
<input type="number" name="math"><br><br>
<label>英语成绩:</label>
<input type="number" name="english"><br><br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
<a href="index.jsp">返回首页</a>
</body>
</html>
```
```html
<!-- search.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body>
<h1>查询结果</h1>
<table border="1">
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>语文成绩</th>
<th>数学成绩</th>
<th>英语成绩</th>
<th>平均分</th>
</tr>
<%
String name = request.getParameter("name");
if(name != null && !"".equals(name)) {
List<Student> students = StudentDAO.search(name);
for(Student student : students) {
out.println("<tr>");
out.println("<td>" + student.getName() + "</td>");
out.println("<td>" + student.getGender() + "</td>");
out.println("<td>" + student.getAge() + "</td>");
out.println("<td>" + student.getChinese() + "</td>");
out.println("<td>" + student.getMath() + "</td>");
out.println("<td>" + student.getEnglish() + "</td>");
out.println("<td>" + student.getAverage() + "</td>");
out.println("</tr>");
}
} else {
out.println("<tr><td colspan='7'>请输入学生姓名</td></tr>");
}
%>
</table>
<a href="index.jsp">返回首页</a>
</body>
</html>
```
2. 后端数据处理
```java
// Student.java
public class Student {
private String name;
private String gender;
private int age;
private int chinese;
private int math;
private int english;
public Student() {}
public Student(String name, String gender, int age, int chinese, int math, int english) {
this.name = name;
this.gender = gender;
this.age = age;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public double getAverage() {
return (chinese + math + english) / 3.0;
}
}
```
```java
// StudentDAO.java
import java.sql.*;
import java.util.*;
public class StudentDAO {
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static List<Student> search(String name) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Student> students = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "select * from student where name like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + name + "%");
rs = pstmt.executeQuery();
while(rs.next()) {
String n = rs.getString("name");
String g = rs.getString("gender");
int a = rs.getInt("age");
int c = rs.getInt("chinese");
int m = rs.getInt("math");
int e = rs.getInt("english");
Student student = new Student(n, g, a, c, m, e);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return students;
}
public static void add(Student student) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "insert into student(name, gender, age, chinese, math, english) values(?, ?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setInt(4, student.getChinese());
pstmt.setInt(5, student.getMath());
pstmt.setInt(6, student.getEnglish());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
```
3. 数据库存储
```sql
-- student.sql
create database student;
use student;
create table student (
id int primary key auto_increment,
name varchar(20) not null,
gender varchar(2) not null,
age int not null,
chinese int not null,
math int not null,
english int not null
);
```
```java
// addStudent.jsp
<%@ page import="Student" %>
<%@ page import="StudentDAO" %>
<%
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
int chinese = Integer.parseInt(request.getParameter("chinese"));
int math = Integer.parseInt(request.getParameter("math"));
int english = Integer.parseInt(request.getParameter("english"));
Student student = new Student(name, gender, age, chinese, math, english);
StudentDAO.add(student);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加成功</title>
</head>
<body>
<h1>添加成功</h1>
<a href="index.jsp">返回首页</a>
</body>
</html>
```
这样,一个简单的学生成绩管理系统就实现了。用户可以通过首页查询学生信息,也可以通过添加学生信息页面添加新的学生信息。所有的数据都存储在MySQL数据库中。
阅读全文