写一个java学生管理系统,GUI界面,链接数据库的代码
时间: 2024-02-12 14:06:08 浏览: 89
好的,下面是一个简单的Java学生管理系统的GUI界面和链接数据库的代码,您可以参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StudentManagementSystemGUI extends JFrame implements ActionListener {
private JLabel nameLabel, ageLabel, genderLabel, idLabel;
private JTextField nameText, ageText, genderText, idText;
private JButton addButton, updateButton, deleteButton, searchButton, clearButton;
private JTextArea resultTextArea;
private Connection conn;
private ResultSet rs;
public StudentManagementSystemGUI() {
// 设置窗口标题
setTitle("学生管理系统");
// 创建组件
nameLabel = new JLabel("姓名:");
ageLabel = new JLabel("年龄:");
genderLabel = new JLabel("性别:");
idLabel = new JLabel("学号:");
nameText = new JTextField(20);
ageText = new JTextField(20);
genderText = new JTextField(20);
idText = new JTextField(20);
addButton = new JButton("添加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
searchButton = new JButton("查询");
clearButton = new JButton("清空");
resultTextArea = new JTextArea(10, 30);
// 添加事件监听器
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
searchButton.addActionListener(this);
clearButton.addActionListener(this);
// 创建布局
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
panel1.add(nameLabel);
panel1.add(nameText);
panel1.add(ageLabel);
panel1.add(ageText);
panel1.add(genderLabel);
panel1.add(genderText);
panel1.add(idLabel);
panel1.add(idText);
JPanel panel2 = new JPanel();
panel2.add(addButton);
panel2.add(updateButton);
panel2.add(deleteButton);
panel2.add(searchButton);
panel2.add(clearButton);
JPanel panel3 = new JPanel();
panel3.add(resultTextArea);
// 将组件添加到窗口上
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
} catch (Exception ex) {
ex.printStackTrace();
}
// 设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new StudentManagementSystemGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
// 处理按钮事件
if (e.getSource() == addButton) {
addStudent();
} else if (e.getSource() == updateButton) {
updateStudent();
} else if (e.getSource() == deleteButton) {
deleteStudent();
} else if (e.getSource() == searchButton) {
searchStudent();
} else if (e.getSource() == clearButton) {
clearFields();
}
}
private void addStudent() {
try {
String name = nameText.getText();
int age = Integer.parseInt(ageText.getText());
String gender = genderText.getText();
String id = idText.getText();
PreparedStatement ps = conn.prepareStatement("INSERT INTO student VALUES (?, ?, ?, ?)");
ps.setString(1, id);
ps.setString(2, name);
ps.setInt(3, age);
ps.setString(4, gender);
int rows = ps.executeUpdate();
if (rows > 0) {
resultTextArea.setText("添加成功!");
} else {
resultTextArea.setText("添加失败!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void updateStudent() {
try {
String name = nameText.getText();
int age = Integer.parseInt(ageText.getText());
String gender = genderText.getText();
String id = idText.getText();
PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?, age=?, gender=? WHERE id=?");
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, gender);
ps.setString(4, id);
int rows = ps.executeUpdate();
if (rows > 0) {
resultTextArea.setText("修改成功!");
} else {
resultTextArea.setText("修改失败!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void deleteStudent() {
try {
String id = idText.getText();
PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?");
ps.setString(1, id);
int rows = ps.executeUpdate();
if (rows > 0) {
resultTextArea.setText("删除成功!");
} else {
resultTextArea.setText("删除失败!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void searchStudent() {
try {
String id = idText.getText();
PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE id=?");
ps.setString(1, id);
rs = ps.executeQuery();
if (rs.next()) {
nameText.setText(rs.getString("name"));
ageText.setText(rs.getString("age"));
genderText.setText(rs.getString("gender"));
} else {
resultTextArea.setText("没有找到该学生!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void clearFields() {
nameText.setText("");
ageText.setText("");
genderText.setText("");
idText.setText("");
resultTextArea.setText("");
}
}
```
这段代码实现了基本的增删改查功能,您可以根据自己的需要修改和完善。同时,需要注意的是,代码中连接的是MySQL数据库,您需要先安装和配置好MySQL,并创建一个名为"student"的数据库,以及一个名为"student"的表,表结构如下:
```
CREATE TABLE student (
id VARCHAR(20) PRIMARY KEY,
name VARCHAR(20),
age INT,
gender VARCHAR(10)
);
```
另外,为了能够成功连接MySQL数据库,您需要下载并添加MySQL的JDBC驱动包到项目中。
阅读全文