用代码学生籍贯信息记录簿(使用数据库) 编制一个学生籍贯(到省一级就行,不用到市)信息记录簿,每个学生信息包括:学号、姓名、籍贯。功能要求: (1)按学号或姓名查询其籍贯; (2)按籍贯查询并输出该籍贯的所有学生; (3)能添加、删除和修改学生的籍贯信息; (4)籍贯从JComboBox中选择。
时间: 2024-03-13 09:43:01 浏览: 109
好的,以下是使用Java和MySQL数据库实现学生籍贯信息记录簿的代码:
```
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class StudentInfoSystem extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:mysql://localhost:3306/student";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private JLabel label_id, label_name, label_native;
private JTextField text_id, text_name;
private JComboBox<String> combo_native;
private JButton button_query, button_add, button_delete, button_modify;
private JTable table;
public StudentInfoSystem() {
super("学生籍贯信息记录簿");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
label_id = new JLabel("学号:");
label_name = new JLabel("姓名:");
label_native = new JLabel("籍贯:");
text_id = new JTextField();
text_name = new JTextField();
combo_native = new JComboBox<>();
button_query = new JButton("查询");
button_add = new JButton("添加");
button_delete = new JButton("删除");
button_modify = new JButton("修改");
JPanel panel_top = new JPanel(new FlowLayout());
panel_top.add(label_id);
panel_top.add(text_id);
panel_top.add(label_name);
panel_top.add(text_name);
panel_top.add(label_native);
panel_top.add(combo_native);
panel_top.add(button_query);
panel_top.add(button_add);
panel_top.add(button_delete);
panel_top.add(button_modify);
button_query.addActionListener(this);
button_add.addActionListener(this);
button_delete.addActionListener(this);
button_modify.addActionListener(this);
DefaultTableModel model = new DefaultTableModel();
model.addColumn("学号");
model.addColumn("姓名");
model.addColumn("籍贯");
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
Container contentPane = getContentPane();
contentPane.add(panel_top, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
setVisible(true);
loadNative();
loadAllData();
}
private void loadNative() {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT DISTINCT native FROM student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String nativeStr = rs.getString("native");
combo_native.addItem(nativeStr);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
private void loadAllData() {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM student";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String nativeStr = rs.getString("native");
Object[] rowData = {id, name, nativeStr};
model.addRow(rowData);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
private void queryData() {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
String id = text_id.getText();
String name = text_name.getText();
String nativeStr = combo_native.getSelectedItem().toString();
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM student WHERE id LIKE '%" + id + "%' AND name LIKE '%" + name + "%' AND native = '" + nativeStr + "'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String studentId = rs.getString("id");
String studentName = rs.getString("name");
String studentNative = rs.getString("native");
Object[] rowData = {studentId, studentName, studentNative};
model.addRow(rowData);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
private void addData() {
String id = text_id.getText();
String name = text_name.getText();
String nativeStr = combo_native.getSelectedItem().toString();
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "INSERT INTO student(id, name, native) VALUES('" + id + "', '" + name + "', '" + nativeStr + "')";
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(this, "添加成功");
loadAllData();
} else {
JOptionPane.showMessageDialog(this, "添加失败");
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
private void deleteData() {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "请选择要删除的学生信息");
return;
}
String id = table.getValueAt(row, 0).toString();
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE id = '" + id + "'";
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(this, "删除成功");
loadAllData();
} else {
JOptionPane.showMessageDialog(this, "删除失败");
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
private void modifyData() {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "请选择要修改的学生信息");
return;
}
String id = table.getValueAt(row, 0).toString();
String name = table.getValueAt(row, 1).toString();
String nativeStr = table.getValueAt(row, 2).toString();
String newNativeStr = combo_native.getSelectedItem().toString();
if (newNativeStr.equals(nativeStr)) {
JOptionPane.showMessageDialog(this, "籍贯未改变,无需修改");
return;
}
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String sql = "UPDATE student SET native = '" + newNativeStr + "' WHERE id = '" + id + "' AND name = '" + name + "'";
int result = stmt.executeUpdate(sql);
if (result > 0) {
JOptionPane.showMessageDialog(this, "修改成功");
loadAllData();
} else {
JOptionPane.showMessageDialog(this, "修改失败");
}
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数据库连接失败");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_query) {
queryData();
} else if (e.getSource() == button_add) {
addData();
} else if (e.getSource() == button_delete) {
deleteData();
} else if (e.getSource() == button_modify) {
modifyData();
}
}
public static void main(String[] args) {
new StudentInfoSystem();
}
}
```
注意事项:
1. 需要下载MySQL数据库驱动,并在程序中加载驱动。
2. 数据库连接信息需要根据实际情况修改。
3. 程序中使用了JDBC技术,需要在程序中进行数据库连接、SQL查询和更新等操作。
阅读全文