开发javaweb的学生管理系统的代码
时间: 2023-09-20 21:11:50 浏览: 100
很抱歉,我不能直接为您提供完整的学生管理系统代码。但是,我可以为您提供一个简单的示例,以帮助您开始编写自己的代码。
这里是一个简单的学生管理系统,使用Java和MySQL数据库:
首先,我们需要创建一个数据库,并在其中创建一个名为“students”的表,该表包含以下列:id(自动递增)、name、age、gender、major。
接下来,我们将创建一个Java类来连接到数据库并执行查询。
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;
public StudentDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}
protected void connect() throws SQLException {
if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(
jdbcURL, jdbcUsername, jdbcPassword);
}
}
protected void disconnect() throws SQLException {
if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}
public List<Student> listAllStudents() throws SQLException {
List<Student> listStudent = new ArrayList<>();
String sql = "SELECT * FROM students";
connect();
Statement statement = jdbcConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
String major = resultSet.getString("major");
Student student = new Student(id, name, age, gender, major);
listStudent.add(student);
}
resultSet.close();
statement.close();
disconnect();
return listStudent;
}
public boolean addStudent(Student student) throws SQLException {
String sql = "INSERT INTO students (name, age, gender, major) VALUES (?, ?, ?, ?)";
connect();
java.sql.PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, student.getName());
statement.setInt(2, student.getAge());
statement.setString(3, student.getGender());
statement.setString(4, student.getMajor());
boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowInserted;
}
public boolean deleteStudent(Student student) throws SQLException {
String sql = "DELETE FROM students where id = ?";
connect();
java.sql.PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, student.getId());
boolean rowDeleted = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowDeleted;
}
public boolean updateStudent(Student student) throws SQLException {
String sql = "UPDATE students SET name = ?, age = ?, gender = ?, major = ?";
sql += " WHERE id = ?";
connect();
java.sql.PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, student.getName());
statement.setInt(2, student.getAge());
statement.setString(3, student.getGender());
statement.setString(4, student.getMajor());
statement.setInt(5, student.getId());
boolean rowUpdated = statement.executeUpdate() > 0;
statement.close();
disconnect();
return rowUpdated;
}
public Student getStudent(int id) throws SQLException {
Student student = null;
String sql = "SELECT * FROM students WHERE id = ?";
connect();
java.sql.PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
String major = resultSet.getString("major");
student = new Student(id, name, age, gender, major);
}
resultSet.close();
statement.close();
return student;
}
}
```
接下来,我们将创建一个名为“Student”的Java类,该类表示学生对象:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String major;
public Student(int id, String name, int age, String gender, String major) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.major = major;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
```
最后,我们将创建一个JSP页面,该页面将使用上面的DAO和Student类来显示学生列表,并允许用户添加、编辑和删除学生。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
</head>
<body>
<h1>学生列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>专业</th>
<th></th>
<th></th>
</tr>
<c:forEach var="student" items="${listStudent}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td>${student.major}</td>
<td><a href="edit?id=${student.id}">编辑</a></td>
<td><a href="delete?id=${student.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<h1>添加学生</h1>
<form action="add" method="post">
<label for="name">姓名:</label><input type="text" name="name" /><br />
<label for="age">年龄:</label><input type="text" name="age" /><br />
<label for="gender">性别:</label><input type="radio" name="gender" value="男" checked>男
<input type="radio" name="gender" value="女">女<br />
<label for="major">专业:</label><input type="text" name="major" /><br />
<input type="submit" value="添加" />
</form>
</body>
</html>
```
这只是一个简单的示例,您可以根据需要进行修改和扩展。祝您编写愉快!
阅读全文