gpt4 book ai didi

java - 更新不适用

转载 作者:行者123 更新时间:2023-11-29 23:07:52 25 4
gpt4 key购买 nike

当我在运行程序时尝试更改变量时,更新不适用。它没有任何错误。说更新记录成功,但是不适用于Mysql。请帮忙!

工作人员DA

 public void actionPerformedUpdate() {
if (StaffDA.updateCustomer(customer)) {
txtfName.setEditable(false);
txtlName.setEditable(false);
txtGender.setEditable(false);
txtEmail.setEditable(false);
txtDateOfBirth.setEditable(false);
txtUserId.setEditable(false);
txtPassword.setEditable(false);
txtContactNumber.setEditable(false);
txtAddress.setEditable(false);

JOptionPane.showMessageDialog(myFrame,
"Record updated successfully", "Alert",
JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(myFrame,
"Database Error. Record not updated.", "Alert",
JOptionPane.ERROR_MESSAGE);
}

}

员工更新面板

    public static boolean updateCustomer(Customer customer) {
//declare local variables
boolean success = false;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;

//step 1 - establish connection to database
db.getConnection();

//step 2 - declare the SQL statement
dbQuery = "UPDATE customer SET fName = ?, lName = ?, gender = ?, email = ?, dateOfBirth = ?, userId = ?, password = ? ,contactNumber = ?, address = ? WHERE id = ?";
pstmt = db.getPreparedStatement(dbQuery);

//step 3 - to update record using executeUpdate method
try {

pstmt.setString(1, customer.getfName());
pstmt.setString(2, customer.getlName());
pstmt.setString(3, customer.getGender());
pstmt.setString(4, customer.getEmail());
pstmt.setString(5, customer.getDateOfBirth());
pstmt.setString(6, customer.getUserId());
pstmt.setString(7, customer.getPassword());
pstmt.setString(8, customer.getContactNumber());
pstmt.setString(9, customer.getAddress());
pstmt.setInt(10, customer.getId());
if (pstmt.executeUpdate() == 1)
success = true;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(success);
//step 4 - close connection
db.terminate();

return success;
}

最佳答案

可能是数据库用户对数据库的访问权限有问题。如果您可以对数据库执行 select 语句,则可以将数据库用户设置为仅具有读取访问权限。

您也可以尝试打印出更新语句并使用同一用户在数据库上手动运行,以查看数据库系统本身是否可以正确运行更新,在这种情况下,您已将问题范围缩小到代码上而不是访问权限。

关于java - 更新不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28213772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com