通过JDBC相关的API实现学生成绩的添加、修改、查询、统计等功能,采用图形界面,数据库采用MySQL,写出相关的sql代码
时间: 2023-12-10 21:40:56 浏览: 107
以下是一个使用Java JDBC和MySQL数据库实现学生成绩的添加、修改、查询、统计等功能的示例代码,包含了图形界面和相关的SQL代码。
先创建一个名为`student`的MySQL数据库,其中包含一个名为`score`的表,该表包含以下字段:`id`(int, 自增主键), `name`(varchar(20)), `math_score`(int), `english_score`(int), `chinese_score`(int)。
SQL代码:
```
CREATE DATABASE student;
USE student;
CREATE TABLE score (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
math_score INT NOT NULL,
english_score INT NOT NULL,
chinese_score INT NOT NULL,
PRIMARY KEY (id)
);
```
Java代码:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class ScoreManager extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField idField, nameField, mathField, englishField, chineseField;
private JButton addButton, modifyButton, deleteButton, queryButton, statButton;
private DefaultTableModel tableModel;
private JTable table;
public ScoreManager() {
super("学生成绩管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
inputPanel.add(new JLabel("学号:"));
idField = new JTextField(10);
inputPanel.add(idField);
inputPanel.add(new JLabel("姓名:"));
nameField = new JTextField(10);
inputPanel.add(nameField);
inputPanel.add(new JLabel("数学成绩:"));
mathField = new JTextField(10);
inputPanel.add(mathField);
inputPanel.add(new JLabel("英语成绩:"));
englishField = new JTextField(10);
inputPanel.add(englishField);
inputPanel.add(new JLabel("语文成绩:"));
chineseField = new JTextField(10);
inputPanel.add(chineseField);
addButton = new JButton("添加");
addButton.addActionListener(this);
inputPanel.add(addButton);
modifyButton = new JButton("修改");
modifyButton.addActionListener(this);
inputPanel.add(modifyButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(this);
inputPanel.add(deleteButton);
queryButton = new JButton("查询");
queryButton.addActionListener(this);
inputPanel.add(queryButton);
statButton = new JButton("统计");
statButton.addActionListener(this);
inputPanel.add(statButton);
add(inputPanel, BorderLayout.NORTH);
tableModel = new DefaultTableModel(new Object[][]{}, new String[]{"学号", "姓名", "数学成绩", "英语成绩", "语文成绩"}) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(780, 480));
add(scrollPane, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addScore();
} else if (e.getSource() == modifyButton) {
modifyScore();
} else if (e.getSource() == deleteButton) {
deleteScore();
} else if (e.getSource() == queryButton) {
queryScore();
} else if (e.getSource() == statButton) {
statScore();
}
}
private void addScore() {
String name = nameField.getText();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入姓名");
return;
}
String mathScore = mathField.getText();
if (!isNumber(mathScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的数学成绩");
return;
}
String englishScore = englishField.getText();
if (!isNumber(englishScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的英语成绩");
return;
}
String chineseScore = chineseField.getText();
if (!isNumber(chineseScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的语文成绩");
return;
}
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root&useSSL=false");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO score (name, math_score, english_score, chinese_score) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, name);
stmt.setInt(2, Integer.parseInt(mathScore));
stmt.setInt(3, Integer.parseInt(englishScore));
stmt.setInt(4, Integer.parseInt(chineseScore));
int affectedRows = stmt.executeUpdate();
if (affectedRows > 0) {
try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next()) {
int id = rs.getInt(1);
Object[] row = {id, name, mathScore, englishScore, chineseScore};
tableModel.addRow(row);
table.setModel(tableModel);
JOptionPane.showMessageDialog(this, "添加成功");
}
}
} else {
JOptionPane.showMessageDialog(this, "添加失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "添加失败");
}
}
private void modifyScore() {
String id = idField.getText();
if (!isNumber(id)) {
JOptionPane.showMessageDialog(this, "请输入正确的学号");
return;
}
String name = nameField.getText();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入姓名");
return;
}
String mathScore = mathField.getText();
if (!isNumber(mathScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的数学成绩");
return;
}
String englishScore = englishField.getText();
if (!isNumber(englishScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的英语成绩");
return;
}
String chineseScore = chineseField.getText();
if (!isNumber(chineseScore)) {
JOptionPane.showMessageDialog(this, "请输入正确的语文成绩");
return;
}
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root&useSSL=false");
PreparedStatement stmt = conn.prepareStatement("UPDATE score SET name = ?, math_score = ?, english_score = ?, chinese_score = ? WHERE id = ?")) {
stmt.setString(1, name);
stmt.setInt(2, Integer.parseInt(mathScore));
stmt.setInt(3, Integer.parseInt(englishScore));
stmt.setInt(4, Integer.parseInt(chineseScore));
stmt.setInt(5, Integer.parseInt(id));
int affectedRows = stmt.executeUpdate();
if (affectedRows > 0) {
int row = table.getSelectedRow();
if (row != -1) {
tableModel.setValueAt(id, row, 0);
tableModel.setValueAt(name, row, 1);
tableModel.setValueAt(mathScore, row, 2);
tableModel.setValueAt(englishScore, row, 3);
tableModel.setValueAt(chineseScore, row, 4);
table.setModel(tableModel);
JOptionPane.showMessageDialog(this, "修改成功");
}
} else {
JOptionPane.showMessageDialog(this, "修改失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "修改失败");
}
}
private void deleteScore() {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(this, "请选择要删除的记录");
return;
}
int id = (int) tableModel.getValueAt(row, 0);
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root&useSSL=false");
PreparedStatement stmt = conn.prepareStatement("DELETE FROM score WHERE id = ?")) {
stmt.setInt(1, id);
int affectedRows = stmt.executeUpdate();
if (affectedRows > 0) {
tableModel.removeRow(row);
table.setModel(tableModel);
JOptionPane.showMessageDialog(this, "删除成功");
} else {
JOptionPane.showMessageDialog(this, "删除失败");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "删除失败");
}
}
private void queryScore() {
String id = idField.getText();
String name = nameField.getText();
String sql = "SELECT * FROM score WHERE 1 = 1";
if (isNumber(id)) {
sql += " AND id = " + id;
}
if (!name.isEmpty()) {
sql += " AND name LIKE '%" + name + "%'";
}
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root&useSSL=false");
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
tableModel.setRowCount(0);
while (rs.next()) {
int idValue = rs.getInt("id");
String nameValue = rs.getString("name");
int mathScore = rs.getInt("math_score");
int englishScore = rs.getInt("english_score");
int chineseScore = rs.getInt("chinese_score");
Object[] row = {idValue, nameValue, mathScore, englishScore, chineseScore};
tableModel.addRow(row);
}
table.setModel(tableModel);
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "查询失败");
}
}
private void statScore() {
String sql = "SELECT AVG(math_score), AVG(english_score), AVG(chinese_score), SUM(math_score), SUM(english_score), SUM(chinese_score) FROM score";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root&useSSL=false");
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
double mathAvg = rs.getDouble(1);
double englishAvg = rs.getDouble(2);
double chineseAvg = rs.getDouble(3);
int mathSum = rs.getInt(4);
int englishSum = rs.getInt(5);
int chineseSum = rs.getInt(6);
JOptionPane.showMessageDialog(this, "数学平均分:" + mathAvg + "\n英语平均分:" + englishAvg + "\n语文平均分:" + chineseAvg + "\n数学总分:" + mathSum + "\n英语总分:" + englishSum + "\n语文总分:" + chineseSum);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "统计失败");
}
}
private boolean isNumber(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
public static void main(String[] args) {
new ScoreManager().setVisible(true);
}
}
```
阅读全文