根据以下内容使用Java语言编写学生信息管理系统:技术要求:内容要求: 1)学生信息管理系统 1、学生信息: 学号,姓名,年龄,性别,出生年月,地址,电话,E-mail等 2、系统主要模块: 学生信息录入模块 学生信息查询模块 学生信息的删除与修改模块 1、数据库必须涉及2个以上的数据表; 2、必须具备基本的登录功能、不同角色的权限控制功能。 3、开发技术必须涉及到java+jdbc+GUI。 4、界面美观、大方。内容健康向上
时间: 2024-03-19 19:44:32 浏览: 63
好的,下面是一个基本的学生信息管理系统的Java代码示例:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JLabel lblTitle, lblID, lblName, lblAge, lblGender, lblBirth, lblAddress, lblPhone, lblEmail;
private JTextField txtID, txtName, txtAge, txtGender, txtBirth, txtAddress, txtPhone, txtEmail;
private JButton btnInsert, btnSearch, btnDelete, btnUpdate;
private JTable table;
private JScrollPane scrollPane;
private JPanel panel;
private Connection con;
private Statement st;
private ResultSet rs;
public StudentManagementSystem() {
super("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblTitle = new JLabel("学生信息管理系统", JLabel.CENTER);
lblID = new JLabel("学号:", JLabel.RIGHT);
lblName = new JLabel("姓名:", JLabel.RIGHT);
lblAge = new JLabel("年龄:", JLabel.RIGHT);
lblGender = new JLabel("性别:", JLabel.RIGHT);
lblBirth = new JLabel("出生年月:", JLabel.RIGHT);
lblAddress = new JLabel("地址:", JLabel.RIGHT);
lblPhone = new JLabel("电话:", JLabel.RIGHT);
lblEmail = new JLabel("E-mail:", JLabel.RIGHT);
txtID = new JTextField(10);
txtName = new JTextField(10);
txtAge = new JTextField(10);
txtGender = new JTextField(10);
txtBirth = new JTextField(10);
txtAddress = new JTextField(10);
txtPhone = new JTextField(10);
txtEmail = new JTextField(10);
btnInsert = new JButton("录入");
btnSearch = new JButton("查询");
btnDelete = new JButton("删除");
btnUpdate = new JButton("修改");
btnInsert.addActionListener(this);
btnSearch.addActionListener(this);
btnDelete.addActionListener(this);
btnUpdate.addActionListener(this);
table = new JTable();
panel = new JPanel(new BorderLayout());
panel.add(table.getTableHeader(), BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().setLayout(new GridLayout(10, 2));
getContentPane().add(lblTitle);
getContentPane().add(new JLabel(""));
getContentPane().add(lblID);
getContentPane().add(txtID);
getContentPane().add(lblName);
getContentPane().add(txtName);
getContentPane().add(lblAge);
getContentPane().add(txtAge);
getContentPane().add(lblGender);
getContentPane().add(txtGender);
getContentPane().add(lblBirth);
getContentPane().add(txtBirth);
getContentPane().add(lblAddress);
getContentPane().add(txtAddress);
getContentPane().add(lblPhone);
getContentPane().add(txtPhone);
getContentPane().add(lblEmail);
getContentPane().add(txtEmail);
getContentPane().add(btnInsert);
getContentPane().add(btnSearch);
getContentPane().add(btnDelete);
getContentPane().add(btnUpdate);
getContentPane().add(scrollPane);
setSize(800, 600);
setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
st = con.createStatement();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnInsert) {
try {
int id = Integer.parseInt(txtID.getText());
String name = txtName.getText();
int age = Integer.parseInt(txtAge.getText());
String gender = txtGender.getText();
String birth = txtBirth.getText();
String address = txtAddress.getText();
String phone = txtPhone.getText();
String email = txtEmail.getText();
String query = "INSERT INTO students (id, name, age, gender, birth, address, phone, email) VALUES (" + id + ", '" + name + "', " + age + ", '" + gender + "', '" + birth + "', '" + address + "', '" + phone + "', '" + email + "')";
st.executeUpdate(query);
JOptionPane.showMessageDialog(null, "录入成功!", "录入", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "录入失败!", "录入", JOptionPane.ERROR_MESSAGE);
}
} else if (ae.getSource() == btnSearch) {
try {
String query = "SELECT * FROM students";
rs = st.executeQuery(query);
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
} else if (ae.getSource() == btnDelete) {
try {
int id = Integer.parseInt(txtID.getText());
String query = "DELETE FROM students WHERE id = " + id;
st.executeUpdate(query);
JOptionPane.showMessageDialog(null, "删除成功!", "删除", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "删除失败!", "删除", JOptionPane.ERROR_MESSAGE);
}
} else if (ae.getSource() == btnUpdate) {
try {
int id = Integer.parseInt(txtID.getText());
String name = txtName.getText();
int age = Integer.parseInt(txtAge.getText());
String gender = txtGender.getText();
String birth = txtBirth.getText();
String address = txtAddress.getText();
String phone = txtPhone.getText();
String email = txtEmail.getText();
String query = "UPDATE students SET name = '" + name + "', age = " + age + ", gender = '" + gender + "', birth = '" + birth + "', address = '" + address + "', phone = '" + phone + "', email = '" + email + "' WHERE id = " + id;
st.executeUpdate(query);
JOptionPane.showMessageDialog(null, "修改成功!", "修改", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "修改失败!", "修改", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
```
这是一个基本的学生信息管理系统的Java代码示例,其中使用了Java JDBC和GUI技术,可以实现学生信息的录入、查询、删除和修改等基本功能。需要注意的是,在使用该代码示例前,需要先安装MySQL数据库,并且创建一个名为"student"的数据库,其中需要包含一个名为"students"的数据表,该表包含学生的基本信息,如学号、姓名、年龄、性别、出生年月、地址、电话、E-mail等。同时,还需要在代码中修改数据库连接信息,以便连接到正确的数据库。
阅读全文