Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 18.02 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package form;
  7. import com.mysql.jdbc.Statement;
  8. import java.awt.HeadlessException;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import javax.swing.JOptionPane;
  14. import koneksi.Koneksi;
  15. import javax.swing.table.DefaultTableModel;
  16.  
  17. /**
  18.  *
  19.  * @author RIDHWAN
  20.  */
  21. public class FormMahasiswa extends javax.swing.JFrame {
  22.  
  23.     /**
  24.      * Creates new form FormMahasiswa
  25.      */
  26.     public FormMahasiswa() {
  27.         initComponents();
  28.         datatable();
  29.     }
  30.    
  31.     public void datatable() {
  32.         DefaultTableModel tbl = new DefaultTableModel();
  33.         tbl.addColumn("NPM");
  34.         tbl.addColumn("Nama");
  35.         tbl.addColumn("Jurusan");
  36.         tbl.addColumn("Jenis Kelamin");
  37.         tbl.addColumn("Alamat");
  38.         tabel.setModel(tbl);
  39.         try {
  40.             Statement statement = (Statement) Koneksi.GetConnection().createStatement();
  41.             ResultSet res = statement.executeQuery("select * from tb_mhswa");
  42.             while(res.next()) {
  43.                 tbl.addRow(new Object[] {
  44.                 res.getString("npm"),
  45.                 res.getString("nama"),
  46.                 res.getString("jurusan"),
  47.                 res.getString("jeniskelamin"),
  48.                 res.getString("alamat")
  49.             });
  50.                 tabel.setModel(tbl);
  51.             }
  52.         } catch (Exception ex) {
  53.             JOptionPane.showMessageDialog(rootPane, "Salah");
  54.         }
  55.        
  56.     }
  57.  
  58.     /**
  59.      * This method is called from within the constructor to initialize the form.
  60.      * WARNING: Do NOT modify this code. The content of this method is always
  61.      * regenerated by the Form Editor.
  62.      */
  63.     @SuppressWarnings("unchecked")
  64.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  65.     private void initComponents() {
  66.  
  67.         jeniskelaminGB = new javax.swing.ButtonGroup();
  68.         jLabel1 = new javax.swing.JLabel();
  69.         jLabel2 = new javax.swing.JLabel();
  70.         jLabel3 = new javax.swing.JLabel();
  71.         jLabel4 = new javax.swing.JLabel();
  72.         jLabel5 = new javax.swing.JLabel();
  73.         jLabel6 = new javax.swing.JLabel();
  74.         jLabel7 = new javax.swing.JLabel();
  75.         npmTF = new javax.swing.JTextField();
  76.         namamhsTF = new javax.swing.JTextField();
  77.         jurusanCB = new javax.swing.JComboBox<>();
  78.         alamatmhsTF = new javax.swing.JTextField();
  79.         jRadioButton1 = new javax.swing.JRadioButton();
  80.         jRadioButton2 = new javax.swing.JRadioButton();
  81.         tambahBT = new javax.swing.JButton();
  82.         simpanBT = new javax.swing.JButton();
  83.         cariBT = new javax.swing.JButton();
  84.         hapusBT = new javax.swing.JButton();
  85.         keluarBT = new javax.swing.JButton();
  86.         jScrollPane1 = new javax.swing.JScrollPane();
  87.         tabel = new javax.swing.JTable();
  88.  
  89.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  90.  
  91.         jLabel1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
  92.         jLabel1.setText("DATA MAHASISWA");
  93.  
  94.         jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  95.         jLabel2.setText("NPM");
  96.  
  97.         jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  98.         jLabel3.setText("Nama Mahasiswa");
  99.  
  100.         jLabel4.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  101.         jLabel4.setText("Jurusan");
  102.  
  103.         jLabel5.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  104.         jLabel5.setText("Jenis Kelamin");
  105.  
  106.         jLabel6.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  107.         jLabel6.setText("Alamat");
  108.  
  109.         jLabel7.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
  110.         jLabel7.setText("Proses");
  111.  
  112.         jurusanCB.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Sistem Informasi", "Teknik Informatika" }));
  113.  
  114.         jeniskelaminGB.add(jRadioButton1);
  115.         jRadioButton1.setText("Laki - Laki");
  116.  
  117.         jeniskelaminGB.add(jRadioButton2);
  118.         jRadioButton2.setText("Wanita");
  119.  
  120.         tambahBT.setText("TAMBAH");
  121.         tambahBT.addActionListener(new java.awt.event.ActionListener() {
  122.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.                 tambahBTActionPerformed(evt);
  124.             }
  125.         });
  126.  
  127.         simpanBT.setText("SIMPAN");
  128.         simpanBT.addActionListener(new java.awt.event.ActionListener() {
  129.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  130.                 simpanBTActionPerformed(evt);
  131.             }
  132.         });
  133.  
  134.         cariBT.setText("CARI");
  135.         cariBT.addActionListener(new java.awt.event.ActionListener() {
  136.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  137.                 cariBTActionPerformed(evt);
  138.             }
  139.         });
  140.  
  141.         hapusBT.setText("HAPUS");
  142.         hapusBT.addActionListener(new java.awt.event.ActionListener() {
  143.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  144.                 hapusBTActionPerformed(evt);
  145.             }
  146.         });
  147.  
  148.         keluarBT.setText("KELUAR");
  149.         keluarBT.addActionListener(new java.awt.event.ActionListener() {
  150.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  151.                 keluarBTActionPerformed(evt);
  152.             }
  153.         });
  154.  
  155.         tabel.setModel(new javax.swing.table.DefaultTableModel(
  156.             new Object [][] {
  157.  
  158.             },
  159.             new String [] {
  160.                 "NPM", "Nama", "Jurusan", "Jenis Kelamin", "Alamant"
  161.             }
  162.         ));
  163.         jScrollPane1.setViewportView(tabel);
  164.  
  165.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  166.         getContentPane().setLayout(layout);
  167.         layout.setHorizontalGroup(
  168.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  169.             .addGroup(layout.createSequentialGroup()
  170.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  171.                     .addGroup(layout.createSequentialGroup()
  172.                         .addGap(38, 38, 38)
  173.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  174.                             .addGroup(layout.createSequentialGroup()
  175.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176.                                     .addComponent(jLabel2)
  177.                                     .addComponent(jLabel4)
  178.                                     .addComponent(jLabel3)
  179.                                     .addComponent(jLabel5)
  180.                                     .addComponent(jLabel6))
  181.                                 .addGap(39, 39, 39)
  182.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  183.                                     .addComponent(jLabel1)
  184.                                     .addComponent(npmTF, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
  185.                                     .addComponent(namamhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
  186.                                     .addComponent(jurusanCB, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
  187.                                     .addComponent(jRadioButton1)
  188.                                     .addComponent(jRadioButton2)
  189.                                     .addComponent(alamatmhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
  190.                                     .addComponent(jLabel7)))
  191.                             .addGroup(layout.createSequentialGroup()
  192.                                 .addComponent(tambahBT)
  193.                                 .addGap(18, 18, 18)
  194.                                 .addComponent(simpanBT)
  195.                                 .addGap(18, 18, 18)
  196.                                 .addComponent(cariBT)
  197.                                 .addGap(18, 18, 18)
  198.                                 .addComponent(hapusBT)
  199.                                 .addGap(18, 18, 18)
  200.                                 .addComponent(keluarBT))))
  201.                     .addGroup(layout.createSequentialGroup()
  202.                         .addGap(13, 13, 13)
  203.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  204.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  205.         );
  206.         layout.setVerticalGroup(
  207.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  208.             .addGroup(layout.createSequentialGroup()
  209.                 .addContainerGap()
  210.                 .addComponent(jLabel1)
  211.                 .addGap(26, 26, 26)
  212.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  213.                     .addComponent(jLabel2)
  214.                     .addComponent(npmTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  215.                 .addGap(18, 18, 18)
  216.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  217.                     .addComponent(jLabel3)
  218.                     .addComponent(namamhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  219.                 .addGap(18, 18, 18)
  220.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  221.                     .addComponent(jLabel4)
  222.                     .addComponent(jurusanCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  223.                 .addGap(18, 18, 18)
  224.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  225.                     .addComponent(jLabel5)
  226.                     .addComponent(jRadioButton1))
  227.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  228.                 .addComponent(jRadioButton2)
  229.                 .addGap(18, 18, 18)
  230.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  231.                     .addComponent(alamatmhsTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  232.                     .addComponent(jLabel6))
  233.                 .addGap(18, 18, 18)
  234.                 .addComponent(jLabel7)
  235.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  236.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  237.                     .addComponent(tambahBT)
  238.                     .addComponent(simpanBT)
  239.                     .addComponent(cariBT)
  240.                     .addComponent(hapusBT)
  241.                     .addComponent(keluarBT))
  242.                 .addGap(18, 18, 18)
  243.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
  244.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  245.         );
  246.  
  247.         pack();
  248.     }// </editor-fold>                        
  249.  
  250.     private void tambahBTActionPerformed(java.awt.event.ActionEvent evt) {                                        
  251.         // TODO add your handling code here:
  252.         npmTF.setText("");
  253.         namamhsTF.setText("");
  254.         jurusanCB.setSelectedItem("");
  255.         if(jRadioButton1.isSelected()) {
  256.             JOptionPane.showMessageDialog(rootPane, "laki - laki");
  257.         } else if (jRadioButton2.isSelected()) {
  258.             JOptionPane.showMessageDialog(rootPane, "wanita");
  259.             alamatmhsTF.setText("");
  260.         }
  261.     }                                        
  262.  
  263.     private void simpanBTActionPerformed(java.awt.event.ActionEvent evt) {                                        
  264.         // TODO add your handling code here:
  265.         String npm = npmTF.getText();
  266.         String nama = namamhsTF.getText();
  267.         String jurusan = (String) jurusanCB.getSelectedItem();
  268.         String alamat = alamatmhsTF.getText();
  269.         String jeniskelamin = null;
  270.         if(jRadioButton1.isSelected()) {
  271.             jeniskelamin = "Laki - Laki";
  272.         } else if(jRadioButton2.isSelected()) {
  273.             jeniskelamin = "Wanita";
  274.         }
  275.        
  276.         try {
  277.             Statement statement = (Statement) Koneksi.GetConnection().createStatement();
  278.             statement.executeUpdate("insert into tb_mhswa VALUES ('"+npm+"','"+nama+"','"+jurusan+"', '"+jeniskelamin+"', '"+alamat+"');");
  279.             statement.close();
  280.             JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
  281.         } catch (Exception ex) {
  282.             JOptionPane.showMessageDialog(null, "Data Gagal Disimpan, "+ ex);
  283.         }
  284.        
  285.         datatable();
  286.     }                                        
  287.  
  288.     private void hapusBTActionPerformed(java.awt.event.ActionEvent evt) {                                        
  289.         // TODO add your handling code here:
  290.         String npm = npmTF.getText();
  291.         try {
  292.             Statement statement = (Statement) Koneksi.GetConnection().createStatement();
  293.             statement.executeUpdate("DELETE from tb_mhswa where npm = ('"+npm+"');");
  294.             JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus");
  295.             npmTF.setText("");
  296.             namamhsTF.setText("");
  297.             jurusanCB.setSelectedItem("");
  298.             String jeniskelamin = null;
  299.             alamatmhsTF.setText("");
  300.             npmTF.requestFocus();
  301.         } catch (Exception ex) {
  302.             JOptionPane.showMessageDialog(null, "Data Gagal Dihapus, " + ex);
  303.         }
  304.     }                                      
  305.  
  306.     private void keluarBTActionPerformed(java.awt.event.ActionEvent evt) {                                        
  307.         // TODO add your handling code here:
  308.         dispose();
  309.     }                                        
  310.  
  311.     private void cariBTActionPerformed(java.awt.event.ActionEvent evt) {                                      
  312.         // TODO add your handling code here:
  313.         try {
  314.             Statement statement = (Statement) Koneksi.GetConnection().createStatement();
  315.             ResultSet res = statement.executeQuery("select * from tb_mhswa where " + "npm='" + npmTF.getText() + "'");
  316.             DefaultTableModel tbl = new DefaultTableModel();
  317.             tbl.addColumn("NPM");
  318.             tbl.addColumn("Nama");
  319.             tbl.addColumn("Jurusan");
  320.            
  321.             tabel.setModel(tbl);
  322.             while (res.next()) {
  323.                 tbl.addRow(new Object[] {
  324.                     res.getString("npm"),
  325.                     res.getString("nama"),
  326.                     res.getString("jurusan")
  327.                 });
  328.                 tabel.setModel(tbl);
  329.             }
  330.         } catch (Exception ex) {
  331.             JOptionPane.showMessageDialog(rootPane, "Salah");
  332.         }
  333.     }                                      
  334.  
  335.     /**
  336.      * @param args the command line arguments
  337.      */
  338.     public static void main(String args[]) {
  339.         /* Set the Nimbus look and feel */
  340.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  341.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  342.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  343.          */
  344.         try {
  345.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  346.                 if ("Nimbus".equals(info.getName())) {
  347.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  348.                     break;
  349.                 }
  350.             }
  351.         } catch (ClassNotFoundException ex) {
  352.             java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  353.         } catch (InstantiationException ex) {
  354.             java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  355.         } catch (IllegalAccessException ex) {
  356.             java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  357.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  358.             java.util.logging.Logger.getLogger(FormMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  359.         }
  360.         //</editor-fold>
  361.  
  362.         /* Create and display the form */
  363.         java.awt.EventQueue.invokeLater(new Runnable() {
  364.             public void run() {
  365.                 new FormMahasiswa().setVisible(true);
  366.             }
  367.         });
  368.     }
  369.  
  370.     // Variables declaration - do not modify                    
  371.     private javax.swing.JTextField alamatmhsTF;
  372.     private javax.swing.JButton cariBT;
  373.     private javax.swing.JButton hapusBT;
  374.     private javax.swing.JLabel jLabel1;
  375.     private javax.swing.JLabel jLabel2;
  376.     private javax.swing.JLabel jLabel3;
  377.     private javax.swing.JLabel jLabel4;
  378.     private javax.swing.JLabel jLabel5;
  379.     private javax.swing.JLabel jLabel6;
  380.     private javax.swing.JLabel jLabel7;
  381.     private javax.swing.JRadioButton jRadioButton1;
  382.     private javax.swing.JRadioButton jRadioButton2;
  383.     private javax.swing.JScrollPane jScrollPane1;
  384.     private javax.swing.ButtonGroup jeniskelaminGB;
  385.     private javax.swing.JComboBox<String> jurusanCB;
  386.     private javax.swing.JButton keluarBT;
  387.     private javax.swing.JTextField namamhsTF;
  388.     private javax.swing.JTextField npmTF;
  389.     private javax.swing.JButton simpanBT;
  390.     private javax.swing.JTable tabel;
  391.     private javax.swing.JButton tambahBT;
  392.     // End of variables declaration                  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement