Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication10;
- import java.awt.Color;
- 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, 800, 600);
- setTitle("Customer");
- getContentPane().setLayout(null);
- // Customer List
- JLabel lblCustomerList = new JLabel("Customer List");
- lblCustomerList.setBounds(350, 44, 87, 14);
- getContentPane().add(lblCustomerList);
- // ScrollPane
- final JScrollPane scrollPane = new JScrollPane();
- scrollPane.setBounds(100, 100, 600, 200);
- getContentPane().add(scrollPane);
- // Table
- table = new JTable();
- scrollPane.setViewportView(table);
- JButton btnInsert = new JButton("Insert");
- btnInsert.setBounds(250, 400, 89, 23);
- btnInsert.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- new aad().setVisible(true);
- setVisible(false);
- }
- });
- getContentPane().add(btnInsert);
- // 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
- System.err.println();
- 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(450, 400, 89, 23);
- getContentPane().add(btnDelete);
- PopulateData();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- }
- private static 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;
- case 7:
- return String.class;
- default:
- return String.class;
- }
- }
- };
- table.setModel(model);
- // Add Column
- String head[] = {"Select", "Name", "Lastname", "Username", "password", "ID", "Birtday", "Gender"};
- for (int i = 0; i < head.length; i++) {
- model.addColumn(head[i]);
- }
- table.setRowHeight(30);
- table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
- table.setForeground(Color.blue);
- Connection connect = null;
- Statement s = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connect = DriverManager.getConnection("jdbc:mysql://localhost/databaseproject", "root", "");
- s = connect.createStatement();
- String sql = "SELECT * FROM customer ORDER BY Name 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("Name"), row, 1);
- model.setValueAt(rec.getString("Lastname"), row, 2);
- model.setValueAt(rec.getString("Username"), row, 3);
- model.setValueAt(rec.getString("password"), row, 4);
- model.setValueAt(rec.getString("ID"), row, 5);
- model.setValueAt(rec.getString("Birdthday"), row, 6);
- model.setValueAt(rec.getString("Gender"), row, 7);
- row++;
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- JOptionPane.showMessageDialog(null, e.getMessage());
- e.printStackTrace();
- }
- try {
- if (s != null) {
- s.close();
- connect.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- 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/databaseproject", "root", "");
- s = connect.createStatement();
- String sql = "DELETE FROM customer WHERE " + " Name = '" + strCustomerID + "' ";
- s.execute(sql);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- JOptionPane.showMessageDialog(null, e.getMessage());
- e.printStackTrace();
- }
- try {
- if (s != null) {
- s.close();
- connect.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment