Java+MySQL实现学生信息管理系统
时间: 2023-12-16 13:03:07 浏览: 109
学生信息管理系统可以分为前端和后端两个部分。前端负责用户界面和交互,后端负责数据存储和处理。本文将介绍如何使用Java和MySQL实现学生信息管理系统。
1. 数据库设计
首先需要设计数据库来存储学生信息。我们可以创建一个名为students的表来存储学生信息,表结构如下:
CREATE TABLE students (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender ENUM('male', 'female') NOT NULL,
birthday DATE NOT NULL,
address VARCHAR(100) NOT NULL,
phone VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
2. 连接数据库
使用Java连接MySQL数据库需要使用JDBC(Java Database Connectivity)驱动程序。可以在maven中添加以下依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
在代码中可以使用以下方式连接数据库:
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
3. 实现增删改查功能
通过连接数据库后,我们可以实现对学生信息的增删改查功能。以下是一个简单的示例代码:
// 添加学生信息
String sql = "INSERT INTO students(name, gender, birthday, address, phone, email) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "张三");
pstmt.setString(2, "male");
pstmt.setDate(3, new java.sql.Date(new Date().getTime()));
pstmt.setString(4, "北京市朝阳区");
pstmt.setString(5, "13812345678");
pstmt.setString(6, "zhangsan@xxx.com");
pstmt.executeUpdate();
// 删除学生信息
String sql = "DELETE FROM students WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 1);
pstmt.executeUpdate();
// 修改学生信息
String sql = "UPDATE students SET name = ? WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "李四");
pstmt.setInt(2, 2);
pstmt.executeUpdate();
// 查询学生信息
String sql = "SELECT * FROM students";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("id") + ", " + rs.getString("name") + ", " + rs.getString("gender") + ", " + rs.getDate("birthday") + ", " + rs.getString("address") + ", " + rs.getString("phone") + ", " + rs.getString("email"));
}
4. 实现用户界面
最后,我们需要实现用户界面来方便用户进行操作。可以使用Java Swing框架来实现用户界面。以下是一个简单的示例代码:
public class StudentManagementSystem extends JFrame {
private JTextField nameField;
private JComboBox<String> genderComboBox;
private JTextField birthdayField;
private JTextField addressField;
private JTextField phoneField;
private JTextField emailField;
public StudentManagementSystem() {
setTitle("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
// 添加学生信息面板
JPanel addPanel = new JPanel();
addPanel.setLayout(new GridLayout(6, 2));
addPanel.setBorder(BorderFactory.createTitledBorder("添加学生信息"));
JLabel nameLabel = new JLabel("姓名:");
nameField = new JTextField();
JLabel genderLabel = new JLabel("性别:");
genderComboBox = new JComboBox<>(new String[] {"男", "女"});
JLabel birthdayLabel = new JLabel("出生日期:");
birthdayField = new JTextField();
JLabel addressLabel = new JLabel("地址:");
addressField = new JTextField();
JLabel phoneLabel = new JLabel("电话:");
phoneField = new JTextField();
JLabel emailLabel = new JLabel("邮箱:");
emailField = new JTextField();
addPanel.add(nameLabel);
addPanel.add(nameField);
addPanel.add(genderLabel);
addPanel.add(genderComboBox);
addPanel.add(birthdayLabel);
addPanel.add(birthdayField);
addPanel.add(addressLabel);
addPanel.add(addressField);
addPanel.add(phoneLabel);
addPanel.add(phoneField);
addPanel.add(emailLabel);
addPanel.add(emailField);
JButton addButton = new JButton("添加");
addButton.addActionListener(e -> {
// 添加学生信息到数据库
});
addPanel.add(addButton);
// 删除学生信息面板
JPanel deletePanel = new JPanel();
deletePanel.setLayout(new GridLayout(2, 2));
deletePanel.setBorder(BorderFactory.createTitledBorder("删除学生信息"));
JLabel idLabel = new JLabel("ID:");
JTextField idField = new JTextField();
deletePanel.add(idLabel);
deletePanel.add(idField);
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(e -> {
// 从数据库删除学生信息
});
deletePanel.add(deleteButton);
// 查询学生信息面板
JPanel queryPanel = new JPanel();
queryPanel.setLayout(new BorderLayout());
queryPanel.setBorder(BorderFactory.createTitledBorder("查询学生信息"));
JTable table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
queryPanel.add(scrollPane, BorderLayout.CENTER);
JButton queryButton = new JButton("查询");
queryButton.addActionListener(e -> {
// 从数据库查询学生信息,并展示在表格中
});
queryPanel.add(queryButton, BorderLayout.SOUTH);
// 将各个面板添加到主界面
getContentPane().setLayout(new GridLayout(1, 3));
getContentPane().add(addPanel);
getContentPane().add(deletePanel);
getContentPane().add(queryPanel);
}
public static void main(String[] args) {
new StudentManagementSystem().setVisible(true);
}
}
以上是一个简单的学生信息管理系统的实现。通过Java和MySQL,我们可以轻松地实现一个功能齐全的学生信息管理系统。
阅读全文