简体   繁体   中英

Unable to update database record in MySQL

I am unable to update a specific record in my database. I am unable to find the error and Eclipse doesn't show any exception error.

Thanks in advance!

Here is my main method

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);
            }

    });

Here is my Data Access method

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;     
}

And here is my Entity

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;
}

}

This is my getPreparedStatement in my DBController class

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;
}   

Here is my 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); 
    } 
}

The strange thing is that I am able to Create and Delete records but not update

What is the result of the method "PaymentDA.updatePayment(e1)", true or false? If true then the dialog "updated" will show else the "not updated" will pop-up.

1) Try storing the result of

PaymentDA.updatePayment(e1)

to a boolean identifier then output it using:

System.out.println(blnResult);

2) Another diagnostic method that you may use is printing the output of affected records by storing in a integer identifier the method:

pstmt.executeUpdate()

to this statement:

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

The output must be only 1, not 0, 2 or more.

3) Third diagnostic that you may try is printing the:

payment.getId()

to assure that the output corresponds or exists to a record in the database

just use simple way

    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);

    }

Just one more ideas what you could check:

Is the id field really unique in your db-schema? Otherwise

 if (pstmt.executeUpdate() == 1)
        success = true;

may be misleading if more than one record is updated. But I suppose you had a look at the database table and checked if there has been any update!?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM