后台:我正在为我的学校项目设计一个简单的GUI应用程序,该应用程序具有基本的CRUD功能,用户可以将基本事务键入相关的文本字段,记录将是分发到数据库中。
问题:现在,Create,Retrieve和Delete功能完美运行,没有任何错误。现在的问题是更新功能不起作用。我无法确定它是JPanel中我的方法中的错误,还是我的数据访问文件中的方法。
注意:所有数据库列表都拼写正确,因为我可以创建记录。
JPanel方法
private void updatingTransaction() {
String tName = UpdateTransaction.txtTName.getText();
String tID = UpdateTransaction.txtTID.getText();
String tDetail = UpdateTransaction.txtTDetail.getText();
String pName = UpdateTransaction.txtPName.getText();
String pNRIC = UpdateTransaction.txtPNRIC.getText();
String pDate = UpdateTransaction.txtPDate.getText();
String pContact = UpdateTransaction.txtPContact.getText();
String pEmail = UpdateTransaction.txtPEmail.getText();
String pOrganisation = UpdateTransaction.txtPOrganisation.getText();
Transaction updateTransaction = new Transaction(tName, tID, tDetail, pName, pNRIC, pDate, pContact, pEmail, pOrganisation);
if (TransactionDA.updateTransaction(updateTransaction)) {
JOptionPane.showMessageDialog(TransFrame, "Record successfully updated.");
} else {
JOptionPane.showMessageDialog(TransFrame, "Sorry, there seems to be an error in updating the record.");
}
}
数据访问方法
public static boolean updateTransaction(Transaction transaction) {
boolean success = false;
DBControl db = new DBControl();
String dbQuery;
PreparedStatement pstmt;
db.getConnection();
dbQuery = "UPDATE transactioninfo SET tName = ?, tID = ?, tDetail = ?, pName = ?, pNRIC = ?, pDate = ?, pContact = ?, pEmail = ?, pOrganisation = ? WHERE id = ?";
pstmt = db.getPreparedStatement(dbQuery);
try {
pstmt.setString(1, transaction.gettName());
pstmt.setString(2, transaction.gettID());
pstmt.setString(3, transaction.gettDetail());
pstmt.setString(4, transaction.getpName());
pstmt.setString(5, transaction.getpNRIC());
pstmt.setString(6, transaction.getpDate());
pstmt.setString(7, transaction.getpContact());
pstmt.setString(8, transaction.getpEmail());
pstmt.setString(9, transaction.getpOrganisation());
pstmt.setInt(10, transaction.getId());
if (pstmt.executeUpdate() == 1)
success = true;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(success);
db.terminate();
return success;
}
答案 0 :(得分:0)
尝试通过添加
来提交连接YourConnection.commit();
pstmt.executeUpdate();
之后答案 1 :(得分:0)
好吧,这些是我对updatesTransaction所做的更改。
private void updateTheTransaction(int id) {
String tName = UpdateTransaction.txtTName.getText();
String tID = UpdateTransaction.txtTID.getText();
String tDetail = UpdateTransaction.txtTDetail.getText();
String pName = UpdateTransaction.txtPName.getText();
String pNRIC = UpdateTransaction.txtPNRIC.getText();
String pDate = UpdateTransaction.txtPDate.getText();
String pContact = UpdateTransaction.txtPContact.getText();
String pEmail = UpdateTransaction.txtPEmail.getText();
String pOrganisation = UpdateTransaction.txtPOrganisation.getText();
Transaction updateTransaction = new Transaction(id+1, tName, tID, tDetail, pName, pNRIC, pDate, pContact, pEmail, pOrganisation);
if (TransactionDA.updateTransaction(updateTransaction)) {
JOptionPane.showMessageDialog(TransFrame, "Record successfully updated.");
} else {
JOptionPane.showMessageDialog(TransFrame, "Sorry, there seems to be an error in updating the record.");
}
}
并且,因为我使用JButton调用该方法,所以我将这些代码行添加到按钮方法
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int selectedIndex = 0;
updateTransaction(selectedIndex);
}
});