keymasterviriya1150

java

Apr 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.44 KB | None | 0 0
  1. package javaapplication10;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.EventQueue;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JTable;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.table.DefaultTableModel;
  16. import javax.swing.JLabel;
  17. import javax.swing.JButton;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.ActionEvent;
  20.  
  21.  
  22. public class MyForm extends JFrame {
  23.  
  24.     static JTable table;
  25.  
  26.     /**
  27.      * Launch the application.
  28.      */
  29.     public static void main(String[] args) {
  30.         EventQueue.invokeLater(new Runnable() {
  31.             public void run() {
  32.                 MyForm frame = new MyForm();
  33.                 frame.setVisible(true);
  34.             }
  35.         });
  36.     }
  37.  
  38.     /**
  39.      * Create the frame.
  40.      */
  41.     public MyForm() {
  42.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         setBounds(100, 100, 800, 600);
  44.         setTitle("Customer");
  45.         getContentPane().setLayout(null);
  46.  
  47.         // Customer List
  48.         JLabel lblCustomerList = new JLabel("Customer List");
  49.        
  50.         lblCustomerList.setBounds(350, 44, 87, 14);
  51.         getContentPane().add(lblCustomerList);
  52.  
  53.         // ScrollPane
  54.         final JScrollPane scrollPane = new JScrollPane();
  55.         scrollPane.setBounds(100, 100, 600, 200);
  56.         getContentPane().add(scrollPane);
  57.  
  58.         // Table
  59.         table = new JTable();
  60.  
  61.         scrollPane.setViewportView(table);
  62.  
  63.        
  64.         JButton btnInsert = new JButton("Insert");
  65.         btnInsert.setBounds(250, 400, 89, 23);
  66.         btnInsert.addActionListener(new ActionListener()
  67.         {
  68.             public void actionPerformed(ActionEvent e)
  69.             {
  70.                    new aad().setVisible(true);
  71.                    setVisible(false);
  72.                    
  73.             }
  74.         });
  75.        
  76.         getContentPane().add(btnInsert);
  77.        
  78.         // Button Delete
  79.         JButton btnDelete = new JButton("Delete");
  80.  
  81.         btnDelete.addActionListener(new ActionListener() {
  82.  
  83.             public void actionPerformed(ActionEvent e) {
  84.  
  85.                 Object[] options = {"Yes", "No"};
  86.                 int n = JOptionPane.showOptionDialog(null, "Do you want to Delete data?", "Confirm to Delete?",
  87.                         JOptionPane.YES_NO_CANCEL_OPTION,
  88.                         JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
  89.                
  90.                 if (n == 0) // Confirm Delete = Yes
  91.                 {
  92.                     for (int i = 0; i < table.getRowCount(); i++) {
  93.                         Boolean chkDel = Boolean.valueOf(table.getValueAt(i, 0).toString()); // Checked
  94.                         System.err.println();
  95.                         if (chkDel) // Checked to Delete
  96.                         {
  97.                             String strCustomerID = table.getValueAt(i, 1).toString(); // get CustomerID
  98.                             DeleteData(strCustomerID); // Delete Data
  99.                         }
  100.                     }
  101.  
  102.                     JOptionPane.showMessageDialog(null, "Delete Data Successfully");
  103.  
  104.                     PopulateData(); // Reload Table
  105.                 }
  106.  
  107.             }
  108.         }
  109.         );
  110.         btnDelete.setBounds(450, 400, 89, 23);
  111.         getContentPane().add(btnDelete);
  112.  
  113.         PopulateData();
  114.          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  115.     }
  116.  
  117.     private static void PopulateData() {
  118.  
  119.         // Clear table
  120.         table.setModel(new DefaultTableModel());
  121.  
  122.         // Model for Table
  123.         DefaultTableModel model = new DefaultTableModel() {
  124.  
  125.             public Class<?> getColumnClass(int column) {
  126.                 switch (column) {
  127.                     case 0:
  128.                         return Boolean.class;
  129.  
  130.                     case 1:
  131.                         return String.class;
  132.  
  133.                     case 2:
  134.                         return String.class;
  135.  
  136.                     case 3:
  137.                         return String.class;
  138.  
  139.                     case 4:
  140.                         return String.class;
  141.  
  142.                     case 5:
  143.                         return String.class;
  144.  
  145.                     case 6:
  146.                         return String.class;
  147.  
  148.                     case 7:
  149.                         return String.class;
  150.                     default:
  151.                         return String.class;
  152.                 }
  153.             }
  154.         };
  155.  
  156.         table.setModel(model);
  157.  
  158.         // Add Column
  159.         String head[] = {"Select", "Name", "Lastname", "Username", "password", "ID", "Birtday", "Gender"};
  160.  
  161.         for (int i = 0; i < head.length; i++) {
  162.             model.addColumn(head[i]);
  163.         }
  164.         table.setRowHeight(30);
  165.         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  166.  
  167.         table.setForeground(Color.blue);
  168.         Connection connect = null;
  169.         Statement s = null;
  170.  
  171.         try {
  172.             Class.forName("com.mysql.jdbc.Driver");
  173.  
  174.             connect = DriverManager.getConnection("jdbc:mysql://localhost/databaseproject", "root", "");
  175.  
  176.             s = connect.createStatement();
  177.  
  178.             String sql = "SELECT * FROM  customer ORDER BY Name  ASC";
  179.  
  180.             ResultSet rec = s.executeQuery(sql);
  181.             int row = 0;
  182.             while ((rec != null) && (rec.next())) {
  183.                 model.addRow(new Object[0]);
  184.                 model.setValueAt(false, row, 0); // Checkbox
  185.                 model.setValueAt(rec.getString("Name"), row, 1);
  186.                 model.setValueAt(rec.getString("Lastname"), row, 2);
  187.                 model.setValueAt(rec.getString("Username"), row, 3);
  188.                 model.setValueAt(rec.getString("password"), row, 4);
  189.                 model.setValueAt(rec.getString("ID"), row, 5);
  190.                 model.setValueAt(rec.getString("Birdthday"), row, 6);
  191.                 model.setValueAt(rec.getString("Gender"), row, 7);
  192.                 row++;
  193.             }
  194.  
  195.         } catch (Exception e) {
  196.             // TODO Auto-generated catch block
  197.             JOptionPane.showMessageDialog(null, e.getMessage());
  198.             e.printStackTrace();
  199.         }
  200.  
  201.         try {
  202.             if (s != null) {
  203.                 s.close();
  204.                 connect.close();
  205.             }
  206.         } catch (SQLException e) {
  207.             // TODO Auto-generated catch block
  208.             e.printStackTrace();
  209.         }
  210.     }
  211.  
  212.     // Delete
  213.     private void DeleteData(String strCustomerID) {
  214.  
  215.         Connection connect = null;
  216.         Statement s = null;
  217.  
  218.         try {
  219.             Class.forName("com.mysql.jdbc.Driver");
  220.  
  221.             connect = DriverManager.getConnection("jdbc:mysql://localhost/databaseproject", "root", "");
  222.  
  223.             s = connect.createStatement();
  224.  
  225.             String sql = "DELETE FROM customer  WHERE " + " Name = '" + strCustomerID + "' ";
  226.             s.execute(sql);
  227.  
  228.         } catch (Exception e) {
  229.             // TODO Auto-generated catch block
  230.             JOptionPane.showMessageDialog(null, e.getMessage());
  231.             e.printStackTrace();
  232.         }
  233.  
  234.         try {
  235.             if (s != null) {
  236.                 s.close();
  237.                 connect.close();
  238.             }
  239.         } catch (SQLException e) {
  240.             // TODO Auto-generated catch block
  241.             System.out.println(e.getMessage());
  242.             e.printStackTrace();
  243.         }
  244.  
  245.     }
  246.  
  247. }
Add Comment
Please, Sign In to add comment