Java 学生管理系统代码
时间: 2024-05-12 09:12:27 浏览: 97
Java 学生管理系统是一个常见的Java课程设计题目,其主要功能是对学生信息进行管理,包括添加学生信息、修改学生信息、删除学生信息以及查询学生信息等操作。具体的代码实现可以根据不同的需求而有所不同,但一般包括以下几个模块:
1. 学生类模块:定义学生类,包括学号、姓名、性别、年龄、班级等属性,并提供相应的get和set方法。
2. 数据库操作模块:包括连接数据库、创建数据库表、插入数据、删除数据、修改数据以及查询数据等操作。
3. 用户界面模块:提供用户界面,让用户可以通过界面完成对学生信息的增删改查等操作。
以下是一个简单的Java学生管理系统代码示例:
```
// 学生类
public class Student {
private String id;
private String name;
private String gender;
private int age;
private String className;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
// 数据库操作类
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT%2B8";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 创建数据库表
public static void createTable() {
Connection conn = getConnection();
Statement stmt = null;
try {
stmt = conn.createStatement();
String sql = "create table if not exists student(id varchar(20), name varchar(20), gender varchar(4), age int, className varchar(20))";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, stmt, null);
}
}
// 插入数据
public static void insert(Student student) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
try {
String sql = "insert into student(id, name, gender, age, className) values(?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getGender());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getClassName());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
// 修改数据
public static void update(Student student) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
try {
String sql = "update student set name=?, gender=?, age=?, className=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getGender());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getClassName());
pstmt.setString(5, student.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
// 删除数据
public static void delete(String id) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
try {
String sql = "delete from student where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, null);
}
}
// 查询数据
public static List<Student> query(String keyword) {
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Student> list = new ArrayList<>();
try {
String sql = "select * from student where name like ? or className like ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyword + "%");
pstmt.setString(2, "%" + keyword + "%");
rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setGender(rs.getString("gender"));
student.setAge(rs.getInt("age"));
student.setClassName(rs.getString("className"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, pstmt, rs);
}
return list;
}
}
// 用户界面类
public class StudentFrame extends JFrame implements ActionListener{
private JTextField tfId, tfName, tfGender, tfAge, tfClassName;
private JButton btnAdd, btnUpdate, btnDelete, btnQuery;
private JTable table;
public StudentFrame() {
initUI();
}
// 初始化用户界面
private void initUI() {
setTitle("学生管理系统");
JPanel panel1 = new JPanel(new GridLayout(5, 2));
panel1.add(new JLabel("学号:"));
tfId = new JTextField(20);
panel1.add(tfId);
panel1.add(new JLabel("姓名:"));
tfName = new JTextField(20);
panel1.add(tfName);
panel1.add(new JLabel("性别:"));
tfGender = new JTextField(20);
panel1.add(tfGender);
panel1.add(new JLabel("年龄:"));
tfAge = new JTextField(20);
panel1.add(tfAge);
panel1.add(new JLabel("班级:"));
tfClassName = new JTextField(20);
panel1.add(tfClassName);
JPanel panel2 = new JPanel(new FlowLayout());
btnAdd = new JButton("添加");
btnAdd.addActionListener(this);
btnUpdate = new JButton("修改");
btnUpdate.addActionListener(this);
btnDelete = new JButton("删除");
btnDelete.addActionListener(this);
btnQuery = new JButton("查询");
btnQuery.addActionListener(this);
panel2.add(btnAdd);
panel2.add(btnUpdate);
panel2.add(btnDelete);
JPanel panel3 = new JPanel(new BorderLayout());
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
panel3.add(scrollPane, BorderLayout.CENTER);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 实现按钮事件处理
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd) { // 添加学生信息
Student student = getStudentFromUI();
DBUtil.insert(student);
} else if (e.getSource() == btnUpdate) { // 修改学生信息
Student student = getStudentFromUI();
DBUtil.update(student);
} else if (e.getSource() == btnDelete) { // 删除学生信息
String id = tfId.getText().trim();
DBUtil.delete(id);
} else if (e.getSource() == btnQuery) { // 查询学生信息
String keyword = tfName.getText().trim();
List<Student> list = DBUtil.query(keyword);
showTableData(list);
}
}
// 从用户界面获取学生信息
private Student getStudentFromUI() {
Student student = new Student();
student.setId(tfId.getText().trim());
student.setName(tfName.getText().trim());
student.setGender(tfGender.getText().trim());
student.setAge(Integer.parseInt(tfAge.getText().trim()));
student.setClassName(tfClassName.getText().trim());
return student;
}
// 显示查询结果到表格中
private void showTableData(List<Student> list) {
DefaultTableModel model = new DefaultTableModel(new Object[][] {}, new Object[] {"学号", "姓名", "性别", "年龄", "班级"});
for (Student student : list) {
Object[] row = {student.getId(), student.getName(), student.getGender(), student.getAge(), student.getClassName()};
model.addRow(row);
}
table.setModel(model);
}
}
// 主程序入口
public class Main {
public static void main(String[] args) {
DBUtil.createTable(); // 创建数据库表
new StudentFrame(); // 启动用户界面
}
}
阅读全文