Minggu, 16 Juli 2017

Aplikasi Java CRUD (Create, Read, Update, Delete)

Aplikasi Java Crud Data dengan Netbeant .dan mysql
Aplikasi crud merupakan dasar pemgraman atau dasar sebuah aplikasi yang digunakan dalam aplikasi proses input dan output dalam hal pengisian formulir dan kemudian akan muncul sebuah laporan. Dengan menguasai aplikasi crud ini maka akan memudahkan dalam hal pembuatan aplikasi lainnya misalkan membuat aplikasi perkantoran sudah tentu didalamnya terkumpul kumpulan crud yang menjadi beberapa inputan..


1 flowchat dari crud 



note: di karnakan flowchat yang saya bikin ke hapus dan sudah tidak ada waktu buat bikin ulang , jadi saya copy paste buatan orang.semoga memakluminya.



Selanjutnya anda perlu membuat database pada mysql console dengan nama database basisdata dan nama tabelnya crud beserta fieldnya yaitu: id, nama, jenis kelamin dan alamat.

bikin Database dengan nama DataSiswa


















2.buat nama aplikasi dengan nama Aplikasi misalkan  Datasiswa.





Buatlah file baru dengan klik kanan pada package -> pilih new -> pilih jframeform kemudian berinama sesuka hati  dan desain seperti gambar dibawah ini.

Variable declaration
private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;




  • source code untuk  manggil data  dari sql untuk tampil di tabel  nya .


private void tampilkan() {
          int row = jTable1.getRowCount();
            for(int s = 0; s<row;s++){
                model.removeRow(0);
            }
        try {
            Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
            ResultSet rs = koneksi.createStatement().executeQuery("select * from datasiswa");
            while (rs.next()){
                String data[]={rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)};
                model.addRow(data);
            }
        } catch (SQLException ex) {
            Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
        }




  • kasih script di button simpan:

 try {

            // TODO add your handling code here:
            Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
            koneksi.createStatement().executeUpdate("insert into datasiswa values"+"('"+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jComboBox1.getSelectedItem()+"','"+jTextField3.getText()+"')");
       tampilkan();
       reset();
        } catch (SQLException ex) {
            Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
        }





  • script untuk button Ganti/edit:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
try {

            // TODO add your handling code here:
            Connection koneksi =DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
            koneksi.createStatement().executeUpdate("update datasiswa set nama='"+jTextField2.getText()+"',jenis_kelamin ='"+jComboBox1.getSelectedItem()+"',alamat ='"+jTextField3.getText()+"' where id ='"+jTextField1.getText()+"'");
         tampilkan();
            reset();
        } catch (SQLException ex) {
            Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
        }



  • script untuk button hapus
 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
   try {

            // TODO add your handling code here:
            Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
            koneksi.createStatement().executeUpdate("delete from datasiswa where id='"+jTextField1.getText()+"'");
        tampilkan();
        reset();
        } catch (SQLException ex) {
            Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);

            }
        }           



    • script untuk button Tambah

     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         


            // TODO add your handling code here:

            tampilkan();
            reset();
            
        }                                        



    • script untuk button cari
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
             int row = jTable1.getRowCount();
                for(int s = 0; s<row;s++){
                    model.removeRow(0);
                }
            try {
                Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                ResultSet rs = koneksi.createStatement().executeQuery("select * from datasiswa where nama='"+jTextField4.getText()+"'");
                while (rs.next()){
                    String data[]={rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)};
                    model.addRow(data);
                }
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }



    • all script
    package data_siswa;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.table.DefaultTableModel;

    /**
     *
     * @author A456UR
     */
    public class crud extends javax.swing.JFrame {
        DefaultTableModel model;
        /**
         * Creates new form crud
         */
        public crud() {
            initComponents();
            String []judul={"ID","NAMA","JENIS KELAMIN","ALAMAT"};
            model= new DefaultTableModel(judul, 0);
            jTable1.setModel(model);
            tampilkan();
            
        }                  

        private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {                                        
            // TODO add your handling code here:
            
        }                                       

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            try {
                // TODO add your handling code here:
                Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                koneksi.createStatement().executeUpdate("insert into datasiswa values"+"('"+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jComboBox1.getSelectedItem()+"','"+jTextField3.getText()+"')");
           tampilkan();
           reset();
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            try {
                // TODO add your handling code here:
                Connection koneksi =DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                koneksi.createStatement().executeUpdate("update datasiswa set nama='"+jTextField2.getText()+"',jenis_kelamin ='"+jComboBox1.getSelectedItem()+"',alamat ='"+jTextField3.getText()+"' where id ='"+jTextField1.getText()+"'");
             tampilkan();
                reset();
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }
                
        }                                        

        private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
            // TODO add your handling code here:
            
            int i=jTable1.getSelectedRow();
            if(1>-1){
                jTextField1.setText(model.getValueAt(1,0).toString());
                jTextField2.setText(model.getValueAt(1,1).toString());
                jComboBox1.setSelectedItem(model.getValueAt(i, 2).toString());
                jTextField3.setText(model.getValueAt(1,3).toString());
            }
        }                                    

        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            try {
                // TODO add your handling code here:
                Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                koneksi.createStatement().executeUpdate("delete from datasiswa where id ='"+jTextField1.getText()+"'");
            tampilkan();
            reset();
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        

        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            tampilkan();
            reset();
            
        }                                        

        private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
             int row = jTable1.getRowCount();
                for(int s = 0; s<row;s++){
                    model.removeRow(0);
                }
            try {
                Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                ResultSet rs = koneksi.createStatement().executeQuery("select * from datasiswa where nama='"+jTextField4.getText()+"'");
                while (rs.next()){
                    String data[]={rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)};
                    model.addRow(data);
                }
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(crud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(crud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(crud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(crud.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>

            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new crud().setVisible(true);
                }
            });
        }

        private void tampilkan() {
              int row = jTable1.getRowCount();
                for(int s = 0; s<row;s++){
                    model.removeRow(0);
                }
            try {
                Connection koneksi =  DriverManager.getConnection("jdbc:mysql://localhost:3306/crud","root","");
                ResultSet rs = koneksi.createStatement().executeQuery("select * from datasiswa");
                while (rs.next()){
                    String data[]={rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)};
                    model.addRow(data);
                }
            } catch (SQLException ex) {
                Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        private void reset(){
            jTextField1.setText("");
             jTextField2.setText("");
             jComboBox1.setSelectedItem("Laki-Laki");
              jTextField3.setText("");
               jTextField4.setText("");
        }
    }





    • Dan ketika di Run dan Coba input data atau coba  klik tiap button lalu kita lihat di database data yang kita input.










    sekian Postingan dari saya mohon maaf bila masih ada kata yang kurang berkenan.
    Saya ucapkan Terima kasih.