Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class gui_edit_customers {
- protected JPanel main_panel;
- private JTextField field_customer_name;
- private JTextField field_customer_surname;
- private JButton confirmButton;
- private JTextField field_customer_email;
- public Connection db_c;
- public Statement db_s;
- public JFrame baseframe;
- public GUI_menu guimenu;
- public int id_to_edit;
- public gui_edit_customers(Connection db_c, Statement db_s, int id_num) {
- this.db_c = db_c;
- this.db_s = db_s;
- this.id_to_edit = id_num;
- String queryText = String.format("SELECT * FROM customers WHERE id = %d", id_num);
- try {
- db_s = db_c.createStatement();
- ResultSet rs = db_s.executeQuery( queryText );
- while ( rs.next() ) {
- field_customer_name.setText(rs.getString("name"));
- field_customer_surname.setText(rs.getString("surname"));
- field_customer_email.setText(rs.getString("email"));
- }
- rs.close();
- db_s.close();
- } catch (Exception e) {
- guimenu.showErrorDialogPopup(e);
- }
- confirmButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent actionEvent) {
- 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);
- try_send(queryText);
- }
- });
- }
- private void try_send(String queryText) {
- try {
- db_c.setAutoCommit(false);
- db_s = db_c.createStatement();
- db_s.executeUpdate(queryText);
- db_c.commit();
- db_s.close();
- guimenu.updateTableModel();
- baseframe.dispose();
- } catch (SQLException se) {
- guimenu.showErrorDialogPopup(se);
- db_c.rollback();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement