Delete with CheckBox Java swing
Java 2016
package com.java.myapp; import java.awt.EventQueue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.JScrollPane; import javax.swing.table.DefaultTableModel; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class MyForm extends JFrame { static JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { MyForm frame = new MyForm(); frame.setVisible(true); } }); } /** * Create the frame. */ public MyForm() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 516, 319); setTitle("ThaiCreate.Com Java GUI Tutorial"); getContentPane().setLayout(null); // Customer List JLabel lblCustomerList = new JLabel("Customer List"); lblCustomerList.setBounds(207, 44, 87, 14); getContentPane().add(lblCustomerList); // ScrollPane JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(28, 84, 440, 89); getContentPane().add(scrollPane); // Table table = new JTable(); scrollPane.setViewportView(table); // Button Delete JButton btnDelete = new JButton("Delete"); btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object[] options = { "Yes", "No" }; int n = JOptionPane .showOptionDialog(null, "Do you want to Delete data?", "Confirm to Delete?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]); if (n == 0) // Confirm Delete = Yes { for (int i = 0; i < table.getRowCount(); i++) { Boolean chkDel = Boolean.valueOf(table.getValueAt(i, 0).toString()); // Checked if(chkDel) // Checked to Delete { String strCustomerID = table.getValueAt(i, 1) .toString(); // get CustomerID DeleteData(strCustomerID); // Delete Data } } JOptionPane.showMessageDialog(null, "Delete Data Successfully"); PopulateData(); // Reload Table } } }); btnDelete.setBounds(205, 202, 89, 23); getContentPane().add(btnDelete); PopulateData(); } private void PopulateData() { // Clear table table.setModel(new DefaultTableModel()); // Model for Table DefaultTableModel model = new DefaultTableModel() { public Class<?> getColumnClass(int column) { switch (column) { case 0: return Boolean.class; case 1: return String.class; case 2: return String.class; case 3: return String.class; case 4: return String.class; case 5: return String.class; case 6: return String.class; default: return String.class; } } }; table.setModel(model); // Add Column model.addColumn("Select"); model.addColumn("CustomerID"); model.addColumn("Name"); model.addColumn("Email"); model.addColumn("CountryCode"); model.addColumn("Budget"); model.addColumn("Used"); Connection connect = null; Statement s = null; try { Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager .getConnection("jdbc:mysql://localhost/mydatabase" + "?user=root&password=root"); s = connect.createStatement(); String sql = "SELECT * FROM customer ORDER BY CustomerID ASC"; ResultSet rec = s.executeQuery(sql); int row = 0; while ((rec != null) && (rec.next())) { model.addRow(new Object[0]); model.setValueAt(false, row, 0); // Checkbox model.setValueAt(rec.getString("CustomerID"), row, 1); model.setValueAt(rec.getString("Name"), row, 2); model.setValueAt(rec.getString("Email"), row, 3); model.setValueAt(rec.getString("CountryCode"), row, 4); model.setValueAt(rec.getFloat("Budget"), row, 5); model.setValueAt(rec.getFloat("Used"), row, 6); row++; } } catch (Exception e) { e.printStackTrace(); } } // Delete private void DeleteData(String strCustomerID) { Connection connect = null; Statement s = null; try { Class.forName("com.mysql.jdbc.Driver"); connect = DriverManager .getConnection("jdbc:mysql://localhost/mydatabase" + "?user=root&password=root"); s = connect.createStatement(); String sql = "DELETE FROM customer WHERE " + "CustomerID = '" + strCustomerID + "' "; s.execute(sql); } catch (Exception e) { e.printStackTrace(); } } }
0 nhận xét:
Post a Comment