当我尝试在运行程序时更改变量时,更新不适用。它没有任何错误。 它表示更新已成功记录,但不适用于Mysql。 请帮忙!
StaffDA
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);
}
}
StaffUpdatePanel
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;
}
答案 0 :(得分:0)
数据库用户对数据库的访问权限可能存在问题。如果可以对数据库执行select语句,则可以将DB用户设置为仅具有读访问权限。
此外,您可以尝试打印更新语句并在同一用户的数据库上手动运行,以查看数据库系统本身是否可以正确运行更新,在这种情况下,您已将问题缩小到代码而不是访问权限。