帮我写出上述代码
时间: 2023-08-06 17:25:04 浏览: 75
js代码-手写js节流
好的,以下是一个使用Java GUI和JDBC实现学生成绩查询功能的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ScoreQueryGUI extends JFrame implements ActionListener {
private JTextField idField;
private JButton queryButton;
private JTable dataTable;
private JScrollPane scrollPane;
private Object[][] data;
private String[] columnNames = {"学号", "姓名", "科目", "成绩"};
public ScoreQueryGUI() {
super("学生成绩查询");
// 创建GUI界面
idField = new JTextField(10);
queryButton = new JButton("查询");
queryButton.addActionListener(this);
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("学号:"));
inputPanel.add(idField);
inputPanel.add(queryButton);
dataTable = new JTable();
scrollPane = new JScrollPane(dataTable);
Container contentPane = getContentPane();
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(scrollPane, BorderLayout.CENTER);
// 设置窗口属性
setSize(500, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == queryButton) {
String id = idField.getText().trim();
if (id.equals("")) {
JOptionPane.showMessageDialog(this, "请输入学号!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
try {
// 连接到数据库
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String user = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, user, password);
// 执行查询
String sql = "SELECT * FROM scores WHERE id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, id);
ResultSet resultSet = statement.executeQuery();
// 解析查询结果
int rowCount = getRowCount(resultSet);
if (rowCount > 0) {
data = new Object[rowCount][4];
int i = 0;
while (resultSet.next()) {
data[i][0] = resultSet.getString("id");
data[i][1] = resultSet.getString("name");
data[i][2] = resultSet.getString("subject");
data[i][3] = resultSet.getFloat("score");
i++;
}
dataTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
} else {
JOptionPane.showMessageDialog(this, "没有找到学号为 " + id + " 的学生!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
// 释放资源
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, "找不到MySQL JDBC驱动程序!", "错误", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "数据库连接出错!", "错误", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
private int getRowCount(ResultSet resultSet) throws SQLException {
int rowCount;
int currentRow = resultSet.getRow();
if (!resultSet.last()) {
rowCount = 0;
} else {
rowCount = resultSet.getRow();
}
if (currentRow == 0) {
resultSet.beforeFirst();
} else {
resultSet.absolute(currentRow);
}
return rowCount;
}
public static void main(String[] args) {
new ScoreQueryGUI();
}
}
```
这段代码使用了Java Swing库来创建GUI界面,使用了JDBC连接到MySQL数据库,并执行了学生成绩查询操作。请注意,这段代码中的数据库连接信息是硬编码在代码中的,您需要将它们修改为您自己的数据库连接信息。
阅读全文