使用java编写程序。(1)设计输入界面,在界面中输入学生信息,保存到数据库; (2)设计删除界面,输入学生学号,如果存在就从数据库中删除; (3)设计更新界面,将第一条学生记录的姓名修改为你的姓名,如果成功则更新显示结果。
时间: 2024-02-25 22:52:42 浏览: 68
好的,下面是一个Java语言实现的示例程序,包括输入界面、删除界面和更新界面,实现了上述三个功能:
```
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentManagementSystem extends JFrame {
private JLabel studentIdLabel, nameLabel, genderLabel, ageLabel;
private JTextField studentIdField, nameField, genderField, ageField;
private JButton addButton, deleteButton, updateButton;
private Connection conn;
private Statement stmt;
public StudentManagementSystem() {
super("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2));
setSize(400, 200);
studentIdLabel = new JLabel("学号");
nameLabel = new JLabel("姓名");
genderLabel = new JLabel("性别");
ageLabel = new JLabel("年龄");
studentIdField = new JTextField(10);
nameField = new JTextField(10);
genderField = new JTextField(10);
ageField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("更新");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateStudent();
}
});
add(studentIdLabel);
add(studentIdField);
add(nameLabel);
add(nameField);
add(genderLabel);
add(genderField);
add(ageLabel);
add(ageField);
add(addButton);
add(deleteButton);
add(updateButton);
setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
private void addStudent() {
String studentId = studentIdField.getText();
String name = nameField.getText();
String gender = genderField.getText();
String age = ageField.getText();
try {
String sql = "INSERT INTO student VALUES ('" + studentId + "', '" + name + "', '" + gender + "', " + age + ")";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "添加成功!");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "添加失败:" + e.getMessage());
}
}
private void deleteStudent() {
String studentId = studentIdField.getText();
try {
String sql = "SELECT * FROM student WHERE student_id='" + studentId + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
sql = "DELETE FROM student WHERE student_id='" + studentId + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "删除成功!");
} else {
JOptionPane.showMessageDialog(this, "学号为" + studentId + "的学生不存在!");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "删除失败:" + e.getMessage());
}
}
private void updateStudent() {
try {
String sql = "SELECT * FROM student LIMIT 1";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
String studentId = rs.getString("student_id");
String name = "你的姓名";
String gender = rs.getString("gender");
String age = rs.getString("age");
sql = "UPDATE student SET name='" + name + "' WHERE student_id='" + studentId + "'";
stmt.executeUpdate(sql);
studentIdField.setText(studentId);
nameField.setText(name);
genderField.setText(gender);
ageField.setText(age);
JOptionPane.showMessageDialog(this, "更新成功!");
} else {
JOptionPane.showMessageDialog(this, "学生信息为空!");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "更新失败:" + e.getMessage());
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
该程序使用Java Swing库来设计GUI界面,使用JDBC连接MySQL数据库来实现学生信息的添加、删除和更新功能。在运行程序之前,请确保已经安装了MySQL数据库,并且在MySQL中创建了名为test的数据库和名为student的表,表结构如下:
```
CREATE TABLE student (
student_id VARCHAR(10) PRIMARY KEY,
name VARCHAR(20),
gender VARCHAR(2),
age INT
);
```
程序中的JDBC连接字符串和数据库用户名、密码需要根据实际情况进行修改。
阅读全文