重新写一个类实现这个功能
时间: 2024-12-23 12:12:46 浏览: 4
为了重写 `RepairResponseUI` 类以实现相同的功能,我们可以优化代码结构和异常处理。以下是改进后的版本:
```java
package test;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class RepairResponseUI extends JFrame {
private JTable repairTable;
private JButton updateButton;
private SqlServerDao sqlServerDao;
public RepairResponseUI() {
sqlServerDao = new SqlServerDao();
setTitle("回应维修界面");
setSize(800, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initializeComponents();
setVisible(true);
}
private void initializeComponents() {
try {
// 获取维修信息并构建表格模型
ResultSet resultSet = sqlServerDao.getRepairInfo();
DefaultTableModel tableModel = buildTableModel(resultSet);
repairTable = new JTable(tableModel);
repairTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(repairTable);
add(scrollPane, BorderLayout.CENTER);
updateButton = new JButton("更新维修信息");
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = repairTable.getSelectedRow();
if (selectedRow != -1) {
// 获取选中行的数据,并进行类型转换
int repairId = (int) repairTable.getValueAt(selectedRow, 0);
String buildingNo = (String) repairTable.getValueAt(selectedRow, 1);
String roomNo = (String) repairTable.getValueAt(selectedRow, 2);
String repairContent = (String) repairTable.getValueAt(selectedRow, 3);
Date reportDate = (Date) repairTable.getValueAt(selectedRow, 6);
Date repairDate = (Date) repairTable.getValueAt(selectedRow, 4);
double amount = (double) repairTable.getValueAt(selectedRow, 5);
String repairPerson = (String) repairTable.getValueAt(selectedRow, 7);
boolean fromRepairFund = (boolean) repairTable.getValueAt(selectedRow, 8);
// 更新维修信息
try {
int rowsAffected = sqlServerDao.updateRepair(repairId, amount, repairPerson, repairDate, buildingNo, roomNo, repairContent, reportDate, fromRepairFund);
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(RepairResponseUI.this, "维修信息更新成功!");
} else {
JOptionPane.showMessageDialog(RepairResponseUI.this, "维修信息更新失败!");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(RepairResponseUI.this, "数据库错误:" + ex.getMessage());
}
} else {
JOptionPane.showMessageDialog(RepairResponseUI.this, "请选择一行进行更新!");
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(updateButton);
add(buttonPanel, BorderLayout.SOUTH);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "获取数据时发生错误:" + e.getMessage());
}
}
private DefaultTableModel buildTableModel(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
Vector<String> columnNames = new Vector<>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
Vector<Vector<Object>> data = new Vector<>();
while (resultSet.next()) {
Vector<Object> vector = new Vector<>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(resultSet.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new RepairResponseUI());
}
}
class SqlServerDao {
public ResultSet getRepairInfo() throws SQLException {
String sql = "SELECT * FROM repairInfo";
Connection conn = getConnection();
Statement stmt = conn.createStatement();
return stmt.executeQuery(sql);
}
public int updateRepair(int repairId, double amount, String repairPerson, Date repairDate, String buildingNo, String roomNo, String repairContent, Date reportDate, boolean fromRepairFund) throws SQLException {
String sql = "UPDATE repairInfo SET BuildingNo = ?, RoomNo = ?, RepairContent = ?, ReportDate = ?, RepairDate = ?, Amount = ?, FromRepairFund = ?, RepairPerson = ? WHERE RepairID = ?";
Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, buildingNo);
stmt.setString(2, roomNo);
stmt.setString(3, repairContent);
stmt.setDate(4, reportDate);
stmt.setDate(5, repairDate);
stmt.setDouble(6, amount);
stmt.setBoolean(7, fromRepairFund);
stmt.setString(8, repairPerson);
stmt.setInt(9, repairId);
int rowsAffected = stmt.executeUpdate();
conn.close();
return rowsAffected;
}
private Connection getConnection() throws SQLException {
// 这里假设使用的是SQL Server连接
// 实际应用中需要替换为具体的连接字符串和驱动
return null; // 示例代码,实际返回数据库连接
}
}
```
### 主要改进点:
1. **异常处理**:在 `initializeComponents` 方法中添加了对 `SQLException` 的捕获和提示。
2. **类型转换**:直接从 `JTable` 中获取所需类型的值,避免了不必要的 `toString` 和 `parseXXX` 转换。
3. **代码结构**:将数据库连接和查询逻辑封装到 `SqlServerDao` 类中,使主类更简洁。
4. **事件处理**:在 `actionPerformed` 方法中,增加了更多的异常处理和用户提示。
这样可以确保代码更加健壮和易于维护。
阅读全文