用java写一个学生管理系统,需要通过javaswing录入和更新学生信息到mysql里,并可以提示重复学号录入
时间: 2024-03-02 09:53:26 浏览: 65
Java+Swing+mysql学生信息管理系统
5星 · 资源好评率100%
好的,我可以为您提供一些思路和代码示例。首先,您需要安装MySQL数据库,并创建一个名为“studentdb”的数据库。然后,您可以创建一个名为“student”的表,其中包含学生ID、姓名、年龄和性别等信息。以下是一个示例表:
```sql
CREATE TABLE student (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
PRIMARY KEY (id)
);
```
接下来,您可以使用Java Swing创建一个图形用户界面(GUI),用于录入和更新学生信息。以下是一个示例GUI:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JLabel idLabel, nameLabel, ageLabel, genderLabel;
private JTextField idField, nameField, ageField, genderField;
private JButton addButton, updateButton;
public StudentManagementSystem() {
super("学生管理系统");
idLabel = new JLabel("学号:");
nameLabel = new JLabel("姓名:");
ageLabel = new JLabel("年龄:");
genderLabel = new JLabel("性别:");
idField = new JTextField(10);
nameField = new JTextField(10);
ageField = new JTextField(10);
genderField = new JTextField(10);
addButton = new JButton("添加");
updateButton = new JButton("更新");
addButton.addActionListener(this);
updateButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(5, 2));
panel.add(idLabel);
panel.add(idField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(ageLabel);
panel.add(ageField);
panel.add(genderLabel);
panel.add(genderField);
panel.add(addButton);
panel.add(updateButton);
setContentPane(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addStudent();
} else if (e.getSource() == updateButton) {
updateStudent();
}
}
private void addStudent() {
String id = idField.getText();
String name = nameField.getText();
String age = ageField.getText();
String gender = genderField.getText();
if (id.equals("") || name.equals("") || age.equals("") || gender.equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整的学生信息!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/studentdb";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student WHERE id='" + id + "'");
if (rs.next()) {
JOptionPane.showMessageDialog(this, "学号已存在,请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
stmt.executeUpdate("INSERT INTO student (id, name, age, gender) VALUES ('" + id + "', '" + name + "', '" + age + "', '" + gender + "')");
JOptionPane.showMessageDialog(this, "添加成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
idField.setText("");
nameField.setText("");
ageField.setText("");
genderField.setText("");
conn.close();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void updateStudent() {
String id = idField.getText();
String name = nameField.getText();
String age = ageField.getText();
String gender = genderField.getText();
if (id.equals("") || name.equals("") || age.equals("") || gender.equals("")) {
JOptionPane.showMessageDialog(this, "请输入完整的学生信息!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/studentdb";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student WHERE id='" + id + "'");
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "学号不存在,请重新输入!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}
stmt.executeUpdate("UPDATE student SET name='" + name + "', age='" + age + "', gender='" + gender + "' WHERE id='" + id + "'");
JOptionPane.showMessageDialog(this, "更新成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
idField.setText("");
nameField.setText("");
ageField.setText("");
genderField.setText("");
conn.close();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
在上面的GUI中,我们使用了一个JPanel来包含所有的组件,然后将其作为内容面板设置到JFrame中。在添加和更新学生信息时,我们使用了JOptionPane来显示提示框。在数据库操作中,我们使用了JDBC API来连接和操作MySQL数据库。
希望这个示例可以帮助您完成学生管理系统的开发。如果您有任何问题,请随时向我提问!
阅读全文