I need help with this update code, I can't figure out how to get it to work.
UserInterface
case 7: //update a Coilover coilNumber = JOptionPane.showInputDialog (null, "What is the Part's Number?", "Part Enquiry", JOptionPane.INFORMATION_MESSAGE); try { Coilovers c = (Coilovers) carpart.executeUpdate (new Coilovers (coilNumber.trim())); int response = JOptionPane.showConfirmDialog (null, c + "\nAre you sure you want to update this record?\n", "Confirm Update?", JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) { try { carpart.updateConfirmed (carpart.find (c)); carpart.sort(); // removing shouldn't alter the order, but sort it just in case!! } catch (NotFoundException nfe) { JOptionPane.showMessageDialog (null, "Part not found - please check Part Number", "Not Found", JOptionPane.INFORMATION_MESSAGE); } catch (SQLException se) { JOptionPane.showMessageDialog (null, "ERROR in databaser", "DATABASE ERROR", JOptionPane.ERROR_MESSAGE); } } else { // Employee record not deleted JOptionPane.showMessageDialog (null, "The Part Record has NOT been Updated", "Not Updated", JOptionPane.INFORMATION_MESSAGE); } } catch (NotFoundException nfe){ JOptionPane.showMessageDialog (null, "Part not found - please check Part Number", "Not Found", JOptionPane.INFORMATION_MESSAGE); } break;
DataStorage
public CarParts executeUpdate (CarParts cp) throws NotFoundException { int index = find (cp); if (index < 0) { throw new NotFoundException (); } else { cp = dStore.get (index); } return cp; } public void updateConfirmed (int index) throws NotFoundException, SQLException { GeneralDBAccess gdba = new GeneralDBAccess(); gdba.update (dStore.get (index)); // remove Person from the database ... dStore.remove (index); // ... and then from ArrayList if no Exceptions are thrown
GeneralDBAccess
public void update (CarParts aPerson) throws NotFoundException, SQLException { if (aPerson instanceof Coilovers) { Coilovers anEmployee = (Coilovers) aPerson; // retrieve the customer attribute values String partName = anEmployee.getPartName(); String amountOwed = (anEmployee.getAmountOwed()); String heightAdjustable = (anEmployee.getHeightAdjustable()); String damperAdjustable = anEmployee.getDamperAdjustable(); String coilNumber = (anEmployee.getCoilNumber()); // define the SQL query statement using the employee number key String sqlUpdate = "UPDATE CoiloversTable " + " SET partName = '" + partName + "', " + " amountOwed = '" + amountOwed + "', " + " heightAdjustable = '" + heightAdjustable + "', " + " damperAdjustable = '" + damperAdjustable + "', " + " WHERE coilNumber = '" + coilNumber + "'"; // see if this customer already exists in the database // NotFoundException & SQLException are thrown by find method find ("Coilovers", coilNumber); // if found, execute the SQL update statement aStatement.executeUpdate (sqlUpdate); } else if (aPerson instanceof Exhaust) { // code for processing Supplier goes here if (aPerson instanceof Exhaust) { Exhaust anEmployee = (Exhaust) aPerson; // retrieve the customer attribute values String partName = anEmployee.getPartName(); String amountOwed = (anEmployee.getAmountOwed()); String material = (anEmployee.getMaterial()); String size = anEmployee.getSize(); String exhaustNumber = (anEmployee.getExhaustNumber()); // define the SQL query statement using the employee number key String sqlUpdate = "UPDATE ExhaustTable " + " SET partName = '" + partName + "', " + " amountOwed = '" + amountOwed + "', " + " material = '" + material + "', " + " size = '" + size + "', " + " WHERE exhaustNumber = '" + exhaustNumber + "'"; } // end if } // end update()