基于mysql 和 IntelliJ设计
使用JAR包:mysql-connector-j-8.0.32.jar
使用JDBC和Java GUI实现学生信息管理系统
1.用户登录:实现一个登录功能,要求用户输入用户名和密码进行验证,并提供合适的反馈信息。
2.学生信息管理:实现对学生信息的增加、修改、删除和查询功能。包括学生的姓名、学号、年龄、性别等信息。
3.数据库连接与操作:使用JDBC连接数据库,并实现对数据库的增、删、改、查等操作。
4.错误处理与异常处理:对用户输入的错误进行适当的处理,并进行异常处理,保证程序的稳定性和安全性。
用户输入用户名和密码进行验证。
验证成功后,跳转到学生信息管理界面。
提供合适的反馈信息,例如登录成功或登录失败的提示。
增加学生信息:用户可以输入学生的姓名、学号、年龄、系别等信息,并将其添加到学生信息表中。
修改学生信息:用户可以选择要修改的学生,并可以更新学生的姓名、学号、年龄、系别等信息。
删除学生信息:用户可以选择要删除的学生,并从学生信息表中删除该学生的记录。
查询学生信息:用户可以根据学号或姓名等条件查询学生信息,并显示符合条件的学生记录。
成绩录入与查询功能:
3.数据库连接与操作功能:
使用JDBC连接数据库:连接到MySQL数据库,以便进行数据库操作。
实现数据库的增、删、改、查等操作:通过JDBC执行SQL语句,实现对学生信息表和成绩表的增、删、改、查等操作。
错误处理与异常处理功能:
4.对用户输入的错误进行适当的处理:例如,验证用户输入的用户名和密码是否符合要求。
进行异常处理:捕获和处理可能发生的异常,以保证程序的稳定性和安全性。
1.JDBC(Java Database Connectivity)是Java语言与数据库之间进行连接和交互的标准API。以下是JDBC的主要流程描述:
2.加载数据库驱动程序:在使用JDBC之前,需要先加载特定数据库的驱动程序。通过使用Class.forName()方法加载数据库驱动程序的类。
3.建立数据库连接:使用DriverManager.getConnection()方法建立与数据库的连接。需要提供数据库的URL、用户名和密码等连接信息。
4.创建Statement或PreparedStatement对象:使用连接对象的createStatement()方法或prepareStatement()方法创建执行SQL语句的Statement或PreparedStatement对象。
5.执行SQL语句:通过调用Statement或PreparedStatement对象的executeQuery()方法(用于执行查询语句)或executeUpdate()方法(用于执行更新、插入或删除语句)来执行SQL语句。
6.处理结果集:如果执行的是查询语句,会返回一个ResultSet对象,可以通过该对象获取查询结果。
7.关闭数据库资源:在使用完数据库连接、Statement对象和ResultSet对象后,需要调用相应的close()方法关闭数据库资源,释放资源和关闭连接。
1.主要使用了Swing库来创建用户界面。以下是涉及到的Java GUI界面设计的要点:
2.使用容器:在界面设计中,使用了JFrame和JPanel等容器来承载其他组件,例如登录界面和学生信息管理界面。
3.使用布局器:为了实现界面的美观和灵活性,使用了不同的布局器来控制组件的位置和大小。常用的布局器包括绝对布局(AbsoluteLayout)、流式布局(FlowLayout)、网格布局(GridLayout)和网格包布局(GridBagLayout)等。
4.使用主要组件:在界面中使用了不同的主要组件,例如菜单栏(JMenuBar)、导航栏(JToolBar)和表格(JTable)等,以便用户进行操作和数据展示。
5.设置组件属性和监听器:通过设置组件的属性,例如文本框(JTextField)的大小、按钮(JButton)的文本等,来定制界面的外观和交互。同时,通过添加监听器(ActionListener、MouseListener等)来响应用户的操作,例如按钮的点击事件。
6.界面跳转:在登录成功后,根据用户权限和操作需求,可以通过界面跳转实现不同模块之间的切换。例如,在登录成功后跳转到学生信息管理界面。
项目的数据库使用MySQL,并包含一个名为stuinfo的表来存储学生信息。下面是对该表的描述:
表名:stuinfo
表结构:
id:整型,非空,主键,用于唯一标识学生记录。
name:字符串,长度为20,用于存储学生的姓名。
age:整型,用于存储学生的年龄。
dept:字符串,长度为20,用于存储学生的系别。
该表用于存储学生的基本信息,每个学生的信息会在表中以一条记录的形式进行存储。通过该表,可以实现学生信息的增加、修改、删除和查询功能。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
public LoginFrame() {
setTitle("登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(300, 200);
setLocationRelativeTo(null); // 居中显示窗口
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
// 添加用户名输入框
JPanel usernamePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(20);
usernamePanel.add(usernameLabel);
usernamePanel.add(usernameField);
panel.add(usernamePanel);
// 添加密码输入框
JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(20);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
panel.add(passwordPanel);
// 添加登录按钮
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("CBR") && password.equals("123456")) {
// 登录成功,关闭登录窗口,打开学生信息管理窗口
JOptionPane.showMessageDialog(LoginFrame.this, "登录成功!");
dispose(); // 关闭登录窗口
new StudentManagementFrame(); // 打开学生信息管理窗口
} else {
JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);
}
}
});
buttonPanel.add(loginButton);
panel.add(buttonPanel);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LoginFrame();
}
});
}
}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
public class StudentManagementFrame extends JFrame {
private JTextField idField;
private JTextField nameField;
private JTextField ageField;
private JTextField deptField;
private DefaultTableModel tableModel;
private JComboBox<String> departmentComboBox;
public StudentManagementFrame() {
setTitle("学生信息管理");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(5, 5, 5, 5);
// 添加学号输入表格
JLabel idLabel = new JLabel("学号:");
idField = new JTextField(20);
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(idLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(idField, constraints);
// 添加姓名输入表格
JLabel nameLabel = new JLabel("姓名:");
nameField = new JTextField(20);
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(nameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(nameField, constraints);
// 添加年龄输入表格
JLabel ageLabel = new JLabel("年龄:");
ageField = new JTextField(20);
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(ageLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(ageField, constraints);
// 添加系别输入表格
JLabel deptLabel = new JLabel("系别:");
deptField = new JTextField(20);
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(deptLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(deptField, constraints);
// 添加按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 2;
panel.add(buttonPanel, constraints);
JButton searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idField.getText();
Student student = getStudent(id);
if (student != null) {
nameField.setText(student.getName());
ageField.setText(String.valueOf(student.getAge()));
deptField.setText(student.getDept());
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息查询成功!");
} else {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "找不到该学生!");
clearFields();
}
}
});
buttonPanel.add(searchButton);
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idField.getText();
String name = nameField.getText();
String ageText = ageField.getText();
String dept = deptField.getText();
if (id.isEmpty() || name.isEmpty() || ageText.isEmpty() || dept.isEmpty()) {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "请输入完整的学生信息!");
return;
}
int age;
try {
age = Integer.parseInt(ageText);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "年龄字段必须为数字!");
return;
}
if (addStudent(id, name, age, dept)) {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息添加成功!");
clearFields();
refreshTable();
} else {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息添加失败!");
}
}
});
buttonPanel.add(addButton);
JButton updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idField.getText();
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String dept = deptField.getText();
if (updateStudent(id, name, age, dept)) {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息修改成功!");
clearFields();
refreshTable();
} else {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息修改失败!");
}
}
});
buttonPanel.add(updateButton);
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idField.getText();
if (deleteStudent(id)) {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息删除成功!");
clearFields();
refreshTable();
} else {
JOptionPane.showMessageDialog(StudentManagementFrame.this, "学生信息删除失败!");
}
}
});
buttonPanel.add(deleteButton);
// 创建专业查询菜单
JLabel departmentLabel = new JLabel("专业:");
departmentComboBox = new JComboBox<>();
departmentComboBox.addItem("所有专业");
departmentComboBox.addItem("计算机科学与技术");
departmentComboBox.addItem("软件工程");
departmentComboBox.addItem("信息管理与信息系统");
departmentComboBox.addItem("电子信息工程");
departmentComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedDepartment = (String) departmentComboBox.getSelectedItem();
if (selectedDepartment.equals("所有专业")) {
refreshTable();
} else {
filterTableByDepartment(selectedDepartment);
}
}
});
constraints.gridx = 0;
constraints.gridy = 5;
panel.add(departmentLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(departmentComboBox, constraints);
// 创建表格模型
tableModel = new DefaultTableModel();
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("年龄");
tableModel.addColumn("系别");
// 创建表格并设置模型
JTable table = new JTable(tableModel);
// 创建滚动面板,将表格放入其中
JScrollPane scrollPane = new JScrollPane(table);
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.fill = GridBagConstraints.BOTH;
panel.add(scrollPane, constraints);
// 初始化表格数据
refreshTable();
// 添加面板到窗口
add(panel);
setVisible(true);
}
private void refreshTable() {
tableModel.setRowCount(0);
List<Student> students = getAllStudents();
for (Student student : students) {
tableModel.addRow(new Object[]{student.getId(), student.getName(), student.getAge(), student.getDept()});
}
}
private void filterTableByDepartment(String department) {
tableModel.setRowCount(0);
List<Student> students = getStudentsByDepartment(department);
for (Student student : students) {
tableModel.addRow(new Object[]{student.getId(), student.getName(), student.getAge(), student.getDept()});
}
}
private List<Student> getStudentsByDepartment(String department) {
List<Student> students = new ArrayList<>();
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("SELECT * FROM stuinfo WHERE dept = ?")) {
statement.setString(1, department);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String dept = resultSet.getString("dept");
Student student = new Student(id, name, age, dept);
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
private boolean addStudent(String id, String name, int age, String dept) {
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("INSERT INTO stuinfo (id, name, age, dept) VALUES (?, ?, ?, ?)")) {
statement.setString(1, id);
statement.setString(2, name);
statement.setInt(3, age);
statement.setString(4, dept);
int rowsAffected = statement.executeUpdate();
return rowsAffected > 0; // 返回是否添加成功
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private boolean updateStudent(String id, String name, int age, String dept) {
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("UPDATE stuinfo SET name = ?, age = ?, dept = ? WHERE id = ?")) {
statement.setString(1, name);
statement.setInt(2, age);
statement.setString(3, dept);
statement.setString(4, id);
int rowsAffected = statement.executeUpdate();
return rowsAffected > 0; // 返回是否修改成功
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private boolean deleteStudent(String id) {
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("DELETE FROM stuinfo WHERE id = ?")) {
statement.setString(1, id);
int rowsAffected = statement.executeUpdate();
return rowsAffected > 0; // 返回是否删除成功
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private Student getStudent(String id) {
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("SELECT * FROM stuinfo WHERE id = ?")) {
statement.setString(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String dept = resultSet.getString("dept");
return new Student(id, name, age, dept);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "123456");
PreparedStatement statement = connection.prepareStatement("SELECT * FROM stuinfo")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String dept = resultSet.getString("dept");
Student student = new Student(id, name, age, dept);
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
private void clearFields() {
idField.setText("");
nameField.setText("");
ageField.setText("");
deptField.setText("");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StudentManagementFrame();
}
});
}
}
public class Student {
private String id;
private String name;
private int age;
private String dept;
public Student(String id, String name, int age, String dept) {
this.id = id;
this.name = name;
this.age = age;
this.dept = dept;
}
// Getters and setters
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getDept() {
return dept;
}
}