Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.SQLException;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. public class gui_edit_customers  {
  11.     protected JPanel main_panel;
  12.     private JTextField field_customer_name;
  13.     private JTextField field_customer_surname;
  14.     private JButton confirmButton;
  15.     private JTextField field_customer_email;
  16.  
  17.     public Connection db_c;
  18.     public Statement db_s;
  19.  
  20.     public JFrame baseframe;
  21.     public GUI_menu guimenu;
  22.  
  23.     public int id_to_edit;
  24.  
  25.     public gui_edit_customers(Connection db_c, Statement db_s, int id_num) {
  26.         this.db_c = db_c;
  27.         this.db_s = db_s;
  28.         this.id_to_edit = id_num;
  29.  
  30.         String queryText = String.format("SELECT * FROM customers WHERE id = %d", id_num);
  31.  
  32.         try {
  33.             db_s = db_c.createStatement();
  34.             ResultSet rs = db_s.executeQuery( queryText );
  35.             while ( rs.next() ) {
  36.                 field_customer_name.setText(rs.getString("name"));
  37.                 field_customer_surname.setText(rs.getString("surname"));
  38.                 field_customer_email.setText(rs.getString("email"));
  39.             }
  40.             rs.close();
  41.             db_s.close();
  42.         } catch (Exception e) {
  43.             guimenu.showErrorDialogPopup(e);
  44.         }
  45.  
  46.         confirmButton.addActionListener(new ActionListener() {
  47.             @Override
  48.             public void actionPerformed(ActionEvent actionEvent) {
  49.                 String queryText = String.format("UPDATE customers SET name = '%s', surname = '%s', email = '%s' WHERE id = %d;", field_customer_name.getText(), field_customer_surname.getText(), field_customer_email.getText(), id_to_edit);
  50.  
  51.                 try_send(queryText);
  52.             }
  53.         });
  54.  
  55.     }
  56.  
  57.     private void try_send(String queryText)  {
  58.         try {
  59.             db_c.setAutoCommit(false);
  60.             db_s = db_c.createStatement();
  61.  
  62.             db_s.executeUpdate(queryText);
  63.             db_c.commit();
  64.  
  65.             db_s.close();
  66.             guimenu.updateTableModel();
  67.             baseframe.dispose();
  68.         } catch (SQLException se) {
  69.             guimenu.showErrorDialogPopup(se);
  70.             db_c.rollback();
  71.  
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement