按文档要求编写完整的Java swing界面和相关的需要在SqlServerDao类中实现的方法
时间: 2024-12-23 16:27:31 浏览: 5
好的,以下是根据文档要求编写的完整Java Swing界面代码以及SqlServerDao
类中的相关方法实现:
Java Swing 界面 (UserPaymentUI.java)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserPaymentUI extends JFrame {
private JTextField buildingNumberField;
private JTextField roomNumberField;
private JButton checkButton;
private JButton payButton;
private JButton cancelButton;
private JLabel unpaidAmountLabel;
public UserPaymentUI() {
setTitle("缴费界面");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
panel.add(new JLabel("楼号:"));
buildingNumberField = new JTextField();
panel.add(buildingNumberField);
panel.add(new JLabel("房号:"));
roomNumberField = new JTextField();
panel.add(roomNumberField);
checkButton = new JButton("检查缴费状态");
checkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
checkPaymentStatus();
}
});
panel.add(checkButton);
unpaidAmountLabel = new JLabel("");
panel.add(unpaidAmountLabel);
payButton = new JButton("一键缴费");
payButton.setEnabled(false);
payButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updatePaidFees();
}
});
panel.add(payButton);
cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
closeAndOpenUserManagementUI();
}
});
panel.add(cancelButton);
add(panel);
setVisible(true);
}
private void checkPaymentStatus() {
String buildingNumber = buildingNumberField.getText();
String roomNumber = roomNumberField.getText();
if (buildingNumber.isEmpty() || roomNumber.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入楼号和房号!");
return;
}
SqlServerDao dao = new SqlServerDao();
boolean hasUnpaidFees = dao.checkPaymentStatus(buildingNumber, roomNumber);
if (!hasUnpaidFees) {
JOptionPane.showMessageDialog(this, "您没有未缴费用!");
} else {
double unpaidAmount = dao.getUnpaidFees(buildingNumber, roomNumber);
unpaidAmountLabel.setText("您的未缴金额为: " + unpaidAmount);
payButton.setEnabled(true);
}
}
private void updatePaidFees() {
String buildingNumber = buildingNumberField.getText();
String roomNumber = roomNumberField.getText();
SqlServerDao dao = new SqlServerDao();
boolean success = dao.updatePaidFees(buildingNumber, roomNumber);
if (success) {
JOptionPane.showMessageDialog(this, "缴费成功!");
unpaidAmountLabel.setText("");
payButton.setEnabled(false);
} else {
JOptionPane.showMessageDialog(this, "缴费失败,请重试!");
}
}
private void closeAndOpenUserManagementUI() {
this.dispose();
new UserManagementUI().setVisible(true);
}
public static void main(String[] args) {
new UserPaymentUI();
}
}
SqlServerDao 类 (SqlServerDao.java)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlServerDao {
private static final String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabase";
private static final String USER = "yourUsername";
private static final String PASS = "yourPassword";
public boolean checkPaymentStatus(String buildingNumber, String roomNumber) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
CallableStatement stmt = conn.prepareCall("{call CheckAllPayments (?, ?)}")) {
stmt.setString(1, buildingNumber);
stmt.setString(2, roomNumber);
ResultSet rs = stmt.executeQuery();
return rs.next(); // 如果有记录,则表示有未缴费用
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public double getUnpaidFees(String buildingNumber, String roomNumber) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
CallableStatement stmt = conn.prepareCall("{call GetUnpaidFees (?, ?, ?)}")) {
stmt.setString(1, buildingNumber);
stmt.setString(2, roomNumber);
stmt.registerOutParameter(3, java.sql.Types.DOUBLE);
stmt.execute();
return stmt.getDouble(3);
} catch (SQLException e) {
e.printStackTrace();
return 0.0;
}
}
public boolean updatePaidFees(String buildingNumber, String roomNumber) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
CallableStatement stmt = conn.prepareCall("{call UpdatePaidFees (?, ?, ?)}")) {
stmt.setString(1, buildingNumber);
stmt.setString(2, roomNumber);
stmt.registerOutParameter(3, java.sql.Types.BOOLEAN);
stmt.execute();
return stmt.getBoolean(3);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
UserManagementUI 类 (UserManagementUI.java)
import javax.swing.*;
public class UserManagementUI extends JFrame {
public UserManagementUI() {
setTitle("用户管理界面");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.add(new JLabel("欢迎来到用户管理界面"));
add(panel);
}
public static void main(String[] args) {
new UserManagementUI().setVisible(true);
}
}
以上代码实现了文档中描述的功能,包括检查缴费状态、显示未缴金额、一键缴费以及返回用户管理界面。请确保数据库连接信息(DB_URL, USER, PASS)正确无误,并且存储过程 CheckAllPayments
, GetUnpaidFees
, 和 UpdatePaidFees
已经在SQL Server中定义。