用Java实现一个简单的班级管理系统网页,给出实现过程,和代码
时间: 2024-03-01 12:50:05 浏览: 156
好的,下面是一个简单的班级管理系统的实现过程和代码示例:
1. 确定需求:本班级管理系统主要包括学生信息的增删改查、课程信息的增删改查、成绩管理等功能。
2. 设计数据库:根据需求设计数据库,包括学生表、课程表、成绩表等。这里我们使用MySQL数据库,具体的表结构如下:
学生表(student):
| id | name | gender | age | phone |
|----|------|--------|-----|-------|
| 学生ID | 学生姓名 | 学生性别 | 学生年龄 | 学生电话 |
课程表(course):
| id | name | teacher | credit |
|----|------|---------|--------|
| 课程ID | 课程名称 | 授课老师 | 学分 |
成绩表(score):
| id | student_id | course_id | score |
|----|-----------|----------|-------|
| 成绩ID | 学生ID | 课程ID | 成绩 |
3. 编写Java代码:根据数据库设计,编写Java代码实现相应的功能,包括连接数据库、增删改查等操作。这里我们使用JDBC来连接数据库,具体的实现代码如下:
```java
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/class_management_system?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
public static void closeConnection() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
```java
import java.sql.*;
public class StudentDao {
public boolean addStudent(Student student) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
boolean flag = false;
String sql = "INSERT INTO student(name, gender, age, phone) VALUES(?, ?, ?, ?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getPhone());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBUtil.closeConnection();
}
return flag;
}
public boolean deleteStudentById(int id) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
boolean flag = false;
String sql = "DELETE FROM student WHERE id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBUtil.closeConnection();
}
return flag;
}
public boolean updateStudent(Student student) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
boolean flag = false;
String sql = "UPDATE student SET name = ?, gender = ?, age = ?, phone = ? WHERE id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getPhone());
pstmt.setInt(5, student.getId());
int count = pstmt.executeUpdate();
if (count > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBUtil.closeConnection();
}
return flag;
}
public Student queryStudentById(int id) {
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
Student student = null;
String sql = "SELECT * FROM student WHERE id = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setPhone(rs.getString("phone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
DBUtil.closeConnection();
}
return student;
}
}
```
4. 设计网页界面:使用HTML、CSS、JavaScript等技术设计网页界面,包括登录界面、学生信息界面、课程信息界面、成绩管理界面等。这里我们以学生信息界面为例,具体的实现代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>学生信息管理</h1>
<form id="form">
<div class="form-group">
<label for="id">ID:</label>
<input type="text" id="id" name="id">
</div>
<div class="form-group">
<label for="name">姓名:</label>
<input type="text" id="name" name="name">
</div>
<div class="form-group">
<label for="gender">性别:</label>
<input type="text" id="gender" name="gender">
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="text" id="age" name="age">
</div>
<div class="form-group">
<label for="phone">电话:</label>
<input type="text" id="phone" name="phone">
</div>
<div class="form-group">
<button type="button" id="add">添加</button>
<button type="button" id="delete">删除</button>
<button type="button" id="update">修改</button>
<button type="button" id="query">查询</button>
</div>
</form>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script src="jquery-3.5.1.min.js"></script>
<script src="app.js"></script>
</body>
</html>
```
5. 将Java代码与网页界面结合:使用Java Servlet技术将Java代码与网页界面结合,实现用户在网页上的操作与数据库的交互。这里我们以学生信息界面为例,具体的实现代码如下:
```java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.google.gson.*;
import java.util.List;
import java.util.ArrayList;
public class StudentServlet extends HttpServlet {
private StudentDao studentDao = new StudentDao();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("add")) {
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String phone = request.getParameter("phone");
Student student = new Student(name, gender, age, phone);
boolean flag = studentDao.addStudent(student);
String result = flag ? "success" : "fail";
response.getWriter().write(result);
} else if (action.equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
boolean flag = studentDao.deleteStudentById(id);
String result = flag ? "success" : "fail";
response.getWriter().write(result);
} else if (action.equals("update")) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String gender = request.getParameter("gender");
int age = Integer.parseInt(request.getParameter("age"));
String phone = request.getParameter("phone");
Student student = new Student(id, name, gender, age, phone);
boolean flag = studentDao.updateStudent(student);
String result = flag ? "success" : "fail";
response.getWriter().write(result);
} else if (action.equals("query")) {
int id = Integer.parseInt(request.getParameter("id"));
Student student = studentDao.queryStudentById(id);
List<Student> list = new ArrayList<Student>();
list.add(student);
String json = new Gson().toJson(list);
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(json);
}
}
}
```
6. 测试与部署:在开发完成后进行测试,确保系统正常运行。然后将系统部署到服务器上,供用户访问。
这是一个简单的班级管理系统的实现过程和代码示例,您可以根据实际情况进行调整和优化。
阅读全文