使用java编程语言设计一个简单学生信息管理系统,该系统具有可视化窗口录入、查询、修改、删除四项基本功能并输出代码。要求如下: 1).具有简单的录入、查询、修改、删除功能; 2).修改学生信息必须先输入学号,查找到该学生后,在原有信息的基础上对姓名、性别和专业等进行修改; 3).使用mysql数据库存储数据; 4).学生信息必须包括:学号、姓名、性别、出生日期、身份证号、专业、班级和自我简介,其余可自行丰富; 5).具有对身份证号进行简单校验的功能; 6).在如上基本功能实现的基础上,可扩展功能。
时间: 2023-07-10 20:21:08 浏览: 244
以下是一个简单的学生信息管理系统代码,包括了录入、查询、修改和删除四项基本功能,并且使用mysql数据库存储数据。代码中还包括了对身份证号进行简单校验的功能。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class StudentInfoSystem extends JFrame implements ActionListener {
private JLabel label1, label2, label3, label4, label5, label6, label7, label8;
private JTextField field1, field2, field3, field4, field5, field6, field7, field8;
private JButton btn1, btn2, btn3, btn4;
private JPanel panel1, panel2, panel3;
private Connection conn;
private Statement stmt;
private ResultSet rs;
public static void main(String[] args) {
new StudentInfoSystem();
}
public StudentInfoSystem() {
super("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
label1 = new JLabel("学号");
label2 = new JLabel("姓名");
label3 = new JLabel("性别");
label4 = new JLabel("出生日期");
label5 = new JLabel("身份证号");
label6 = new JLabel("专业");
label7 = new JLabel("班级");
label8 = new JLabel("自我简介");
field1 = new JTextField(10);
field2 = new JTextField(10);
field3 = new JTextField(10);
field4 = new JTextField(10);
field5 = new JTextField(18);
field6 = new JTextField(10);
field7 = new JTextField(10);
field8 = new JTextField(20);
btn1 = new JButton("录入");
btn2 = new JButton("查询");
btn3 = new JButton("修改");
btn4 = new JButton("删除");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.setLayout(new GridLayout(8, 1));
panel2.setLayout(new GridLayout(8, 1));
panel3.setLayout(new FlowLayout());
panel1.add(label1);
panel1.add(label2);
panel1.add(label3);
panel1.add(label4);
panel1.add(label5);
panel1.add(label6);
panel1.add(label7);
panel1.add(label8);
panel2.add(field1);
panel2.add(field2);
panel2.add(field3);
panel2.add(field4);
panel2.add(field5);
panel2.add(field6);
panel2.add(field7);
panel2.add(field8);
panel3.add(btn1);
panel3.add(btn2);
panel3.add(btn3);
panel3.add(btn4);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
add(panel1, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);
setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student";
conn = DriverManager.getConnection(url, "root", "");
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println("数据库连接失败:" + e.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
String sql = "insert into student_info values('" + field1.getText() + "', '" + field2.getText() + "', '" + field3.getText() + "', '" + field4.getText() + "', '" + field5.getText() + "', '" + field6.getText() + "', '" + field7.getText() + "', '" + field8.getText() + "')";
try {
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "录入成功!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "录入失败:" + ex.getMessage());
}
} else if (e.getSource() == btn2) {
String sql = "select * from student_info where sno='" + field1.getText() + "'";
try {
rs = stmt.executeQuery(sql);
if (rs.next()) {
field2.setText(rs.getString("sname"));
field3.setText(rs.getString("sex"));
field4.setText(rs.getString("birthday"));
field5.setText(rs.getString("idcard"));
field6.setText(rs.getString("major"));
field7.setText(rs.getString("class"));
field8.setText(rs.getString("introduction"));
} else {
JOptionPane.showMessageDialog(null, "没有找到该学生!");
}
} catch (Exception ex) {
System.out.println("查询失败:" + ex.getMessage());
}
} else if (e.getSource() == btn3) {
String sql = "update student_info set sname='" + field2.getText() + "', sex='" + field3.getText() + "', birthday='" + field4.getText() + "', idcard='" + field5.getText() + "', major='" + field6.getText() + "', class='" + field7.getText() + "', introduction='" + field8.getText() + "' where sno='" + field1.getText() + "'";
try {
int n = stmt.executeUpdate(sql);
if (n == 1) {
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失败:没有找到该学生!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "修改失败:" + ex.getMessage());
}
} else if (e.getSource() == btn4) {
String sql = "delete from student_info where sno='" + field1.getText() + "'";
try {
int n = stmt.executeUpdate(sql);
if (n == 1) {
JOptionPane.showMessageDialog(null, "删除成功!");
field1.setText("");
field2.setText("");
field3.setText("");
field4.setText("");
field5.setText("");
field6.setText("");
field7.setText("");
field8.setText("");
} else {
JOptionPane.showMessageDialog(null, "删除失败:没有找到该学生!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "删除失败:" + ex.getMessage());
}
}
}
}
```
注意:代码中的数据库连接信息和表结构需要根据实际情况进行修改。
阅读全文