0

I am doing an update form. The update button hangs once I click it, but the data still gets successfully updated in my database. My code is

String url = "jdbc:derby://localhost:1527/Members";
String username = "admin1";
String password = "admin1";

String query = "UPDATE BOOKINGDATA SET BOOKINGREF = ?, MEMBERID = ?, NAME = ?, CONTACT = ?, EMAILADDRESS = ?, RESERVATIONDATE = ?, RESERVATIONTIME = ? WHERE BOOKINGREF = ?";

try (Connection con = DriverManager.getConnection(url, username, password);
    PreparedStatement ps = con.prepareStatement(query)) {

    ps.setString(1, txtBookingRef.getText());
    ps.setString(2, txtMemberID.getText());
    ps.setString(3, txtName.getText());
    ps.setString(4, txtContact.getText());
    ps.setString(5, txtEmail.getText());
    ps.setDate  (6, new java.sql.Date(comboDate.getDate().getTime()));
    ps.setString(7, comboTime.getSelectedItem().toString());
    ps.setString(8, txtBookingRef.getText());

    ps.executeUpdate();

    JOptionPane.showMessageDialog(null, "Booking updated");

} catch(SQLException ex) {
    JOptionPane.showMessageDialog(null, ex.toString());
}

Any ideas?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
xxpfb
  • 3
  • 1
  • 4
  • 3
    Are you executing this code in the button's `ActionListener` or otherwise on the event dispatcher thread (EDT)? Doing things like file I/O, network calls or database queries on the EDT is a big no-no, and is always a sure way to make a Java (and most other) UI applications hang. Instead, use a `SwingWorker`. – Harald K Feb 01 '16 at 18:31
  • Yes, it's within the button's ActionListener. Actually, rather than hanging, the run time is taking a long time and thereafter returned this error: `java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested`. – xxpfb Feb 01 '16 at 18:42
  • 2
    See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for the reasons why and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for a possible solution – MadProgrammer Feb 01 '16 at 20:35
  • For [example](http://stackoverflow.com/questions/20944719/how-to-use-swingworker/20945255#20945255) – MadProgrammer Feb 01 '16 at 20:39

0 Answers0