无法更新MySQL中的数据库记录

时间:2016-08-06 18:28:43

标签: java mysql

我无法更新数据库中的特定记录。我无法找到错误,Eclipse也没有显示任何异常错误。

提前致谢!

这是我的主要方法

public void actionPerformed(ActionEvent arg0) {
        int id = PaymentRecord.id;
        int transID = Integer.parseInt(TransactionTxt.getText());
        double totalPay = Double.parseDouble(lblTotalPay.getText());
        double medAmt = Double.parseDouble(MedicineAmountTxt.getText());
            PaymentEntity e1 = new PaymentEntity(id, NameTxt.getText(), transID, DateTxt.getText(), PaymentTxt.getText(), medAmt, MedAdmnTxt.getText(), RoomTxt.getText(),ServicesTxt.getText(),totalPay);
            if (PaymentDA.updatePayment(e1)) {
                JOptionPane.showMessageDialog(HMSFrame,"Record created successfully", "Alert",JOptionPane.INFORMATION_MESSAGE);
                NameTxt.setText("");
                TransactionTxt.setText("");
                DateTxt.setText("");
                PaymentTxt.setText("");
                ConsultationAmountTxt.setText("");
                ConsultationSubsidyTxt.setText("");
                MedicineAmountTxt.setText("");
                MedicineSubsidyTxt.setText("");
                RoomAmountTxt.setText("");
                RoomSubsidyTxt.setText("");
                ServicesAmountTxt.setText("");
                ServicesSubsidyTxt.setText("");
                lblTotalPay.setText("");
                MedAdmnTxt.setText("");
                lblMedAmtTxt.setText("");
                ServicesTxt.setText("");
                RoomTxt.setText("");

            } else
                JOptionPane.showMessageDialog(HMSFrame,"Sorry. Record not updated.", "Alert",JOptionPane.ERROR_MESSAGE);
            }

    });

这是我的数据访问方法

public static boolean updatePayment(PaymentEntity payment) {
    boolean success = false;
    DBController db = new DBController();
    String dbQuery; 
    PreparedStatement pstmt;

    db.getConnection();     

    dbQuery = "UPDATE payment.paymentdetail SET name = ?, transactionID = ?, dateOfTransaction = ?, paymentMethod = ?, medicineAmount = ?, medicineAdministered = ?, roomType = ?, servicesOffered = ?, amountDue = ? WHERE id = ?";
    pstmt = db.getPreparedStatement(dbQuery);

    try {
        pstmt.setString(1, payment.getName());
        pstmt.setInt(2, payment.getTransactionID());
        pstmt.setString(3, payment.getDateOfTransaction());
        pstmt.setString(4, payment.getPaymentMethod());
        pstmt.setDouble(5, payment.getMedicineAmount());
        pstmt.setString(6, payment.getMedicineAdministered());
        pstmt.setString(7, payment.getroomType());
        pstmt.setString(8, payment.getServicesOffered());
        pstmt.setDouble(9, payment.getAmountDue());
        pstmt.setInt(10, payment.getid());
        if (pstmt.executeUpdate() == 1)
            success = true;
        pstmt.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(success);
    db.terminate();

    return success;     
}

这是我的实体

public class PaymentEntity {

int id;
String name;
int transactionID;
String dateOfTransaction;
String paymentMethod;
double amountDue;
double medicineAmount;
String medicineAdministered;
String roomType;
String servicesOffered;

public PaymentEntity(int id, String name,int transactionID, String dateOfTransaction, String paymentMethod, double amountDue,
        String roomType, String medicineAdministered, String servicesOffered, double medicineAmount) {
    super();
    this.id = id;
    this.name = name;
    this.transactionID = transactionID;
    this.dateOfTransaction = dateOfTransaction;
    this.paymentMethod = paymentMethod;
    this.amountDue = amountDue;
    this.medicineAmount = medicineAmount;
    this.roomType = roomType;
    this.servicesOffered = servicesOffered;
    this.medicineAdministered = medicineAdministered;
}

public int getid() {
    return id;
}
public void setid(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getTransactionID() {
    return transactionID;
}
public void setTransactionID(int transactionID) {
    this.transactionID = transactionID;
}
public String getDateOfTransaction() {
    return dateOfTransaction;
}
public void setDateOfTransaction(String dateOfTransaction) {
    this.dateOfTransaction = dateOfTransaction;
}
public String getPaymentMethod() {
    return paymentMethod;
}
public void setPaymentMethod(String paymentMethod) {
    this.paymentMethod = paymentMethod;
}
public double getAmountDue() {
    return amountDue;
}
public void setAmountDue(double amountDue) {
    this.amountDue = amountDue;
}
public double getMedicineAmount() {
    return medicineAmount;
}
public void setMedicineAmount(double medicineAmount) {
    this.medicineAmount = medicineAmount;
}
public String getroomType() {
    return roomType;
}
public void setroomType(String roomType) {
    this.roomType = roomType;
}
public String getServicesOffered() {
    return servicesOffered;
}
public void setServicesOffered(String servicesOffered) {
    this.servicesOffered = servicesOffered;
}
public String getMedicineAdministered() {
    return medicineAdministered;
}
public void setMedicineAdministered(String medicineAdministered) {
    this.medicineAdministered = medicineAdministered;
}

}

这是我的DBController类中的getPreparedStatement

public PreparedStatement getPreparedStatement(String dbQuery) {
    PreparedStatement pstmt = null;
    System.out.println("DB prepare statement: " + dbQuery);
    try {
        // create a statement object
        pstmt = con.prepareStatement(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pstmt;
}   

这是我的getConnection

public void getConnection(){ 
    String url = ""; 
    try { 
        url = "jdbc:mysql://127.0.0.1/patient"; 
        con = DriverManager.getConnection(url, "root", "IT1639"); 
        System.out.println("Successfully connected to " + url+ "."); 
    } 
    catch (java.sql.SQLException e) { 
        System.out.println("Connection failed! ->"+ url); 
        System.out.println(e); 
    } 
}

奇怪的是我能够创建和删除记录但不能更新

3 个答案:

答案 0 :(得分:1)

该方法的结果是什么" PaymentDA.updatePayment(e1)",是或否?如果为true,则对话框"更新"将显示其他"未更新"会弹出。

1)尝试存储

的结果
PaymentDA.updatePayment(e1)

到布尔标识符,然后使用:

输出
System.out.println(blnResult);

2)您可以使用的另一种诊断方法是通过以整数标识符存储方法来打印受影响记录的输出:

pstmt.executeUpdate()

发表声明:

int intRowsAffected=pstmt.executeUpdate();
System.out.println(intRowsAffected);

输出必须仅为1,而不是0,2或更多。

3)您可以尝试的第三种诊断是打印:

payment.getId()

确保输出对应或存在于数据库中的记录

答案 1 :(得分:1)

只需使用简单的方法

    try{

          PreparedStatement stmt = obj.conn.prepareStatement("update query here");
           stmt.executeUpdate();
           JOptionPane.showMessageDialog(HMSFrame,"Record created successfully", "Alert",JOptionPane.INFORMATION_MESSAGE);

    }
    catch(SQLException e){
            JOptionPane.showMessageDialog(HMSFrame,e.getMessage(), "Alert",JOptionPane.ERROR_MESSAGE);

    }

答案 2 :(得分:1)

还有一个想法,你可以检查:

id字段在db-schema中是否真的独一无二?否则

 if (pstmt.executeUpdate() == 1)
        success = true;
如果更新了多条记录,

可能会产生误导。 但是我想你看了一下数据库表并检查是否有任何更新!?