使用java写一个使用面向对象编程方法的学生管理系统(包含数据库增删改查)
时间: 2024-01-14 16:05:07 浏览: 90
好的,我可以为您提供一个简单的示例代码,实现一个学生管理系统,包含基本的增删改查功能,使用面向对象编程方法,并使用Java语言和MySQL数据库。
首先,我们需要定义一个学生类,包含学生的基本信息,例如姓名、学号、年龄、性别等等。代码如下:
```java
public class Student {
private String name;
private int studentId;
private int age;
private String gender;
public Student(String name, int studentId, int age, String gender) {
this.name = name;
this.studentId = studentId;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getStudentId() {
return studentId;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
```
接下来,我们需要定义一个学生管理系统类,包含对学生信息的增删改查等操作。代码如下:
```java
import java.sql.*;
public class StudentManagementSystem {
private Connection conn;
private Statement stmt;
public StudentManagementSystem(String url, String username, String password) throws SQLException {
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
createTable();
}
private void createTable() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS students " +
"(name VARCHAR(255), student_id INT PRIMARY KEY, age INT, gender VARCHAR(255))";
stmt.executeUpdate(sql);
}
public void addStudent(Student student) throws SQLException {
String sql = "INSERT INTO students (name, student_id, age, gender) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getStudentId());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getGender());
pstmt.executeUpdate();
pstmt.close();
}
public void deleteStudent(int studentId) throws SQLException {
String sql = "DELETE FROM students WHERE student_id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, studentId);
pstmt.executeUpdate();
pstmt.close();
}
public void updateStudent(int studentId, String column, Object value) throws SQLException {
String sql = "UPDATE students SET " + column + " = ? WHERE student_id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, value);
pstmt.setInt(2, studentId);
pstmt.executeUpdate();
pstmt.close();
}
public Student searchStudent(int studentId) throws SQLException {
String sql = "SELECT * FROM students WHERE student_id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, studentId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
return new Student(name, studentId, age, gender);
}
return null;
}
}
```
这里我们使用了MySQL数据库来存储学生信息,对应的数据库表为`students`。
最后,我们可以使用以下代码来测试学生管理系统的功能:
```java
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "password";
try {
StudentManagementSystem sms = new StudentManagementSystem(url, username, password);
// 添加学生
Student student1 = new Student("Tom", 10001, 18, "Male");
sms.addStudent(student1);
// 修改学生信息
sms.updateStudent(10001, "name", "Jerry");
// 查询学生信息
Student student = sms.searchStudent(10001);
if (student != null) {
System.out.println(student.getName() + " " + student.getStudentId() + " " + student.getAge() + " " + student.getGender());
}
// 删除学生
sms.deleteStudent(10001);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上代码仅为示例,实际的学生管理系统还可以添加更多功能,例如批量导入、导出学生信息等等。
阅读全文