Pages

Aplikasi CRUD di Netbean dengan Database ( Database Buku )

Nama : Henry Taswin
NIM   : 161021450263
Kelas : 02TPLE004 / 514

Judul Aplikasi : Aplikasi CRUD di Netbean dengan Database ( Database Buku )

Deskripsi : Aplikasi CRUD Database Buku yang terhubung dengan Database MySql dan menggunakan PhpMyAdmin sebagai databasenya. Aplikasi sederhana ini dapat menyimpan, membaca, update dan mendelete database dari aplikasi GUI yang dibuat di JAVA Netbeans. Sebagai tahap awal untuk aplikasi stock toko buku.


Main Menu


PhpMyAdmin Database


Delete Data dari Aplikasi


Flow Chart :











Source Code :


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import java.sql.ResultSet;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

/**

 *

 * @author Henry Taswin

 * NIM: 161021450263

 * Kelas : 02TPLE004

 */

public class Utama_BookDB extends javax.swing.JFrame {

    private Connection con;

    private Statement stat;

    private ResultSet res;

    

    /**

     * Creates new form Utama_BookDB

     */

    public Utama_BookDB() {

        initComponents();

     //   setTitle("CRUD Database Buku");

        koneksi();

        tabel();

        kosongkan();

        ClearTabel();

      

    }

    

        // menghubungkan dengan database

    private void koneksi(){

           try {

              Class.forName("com.mysql.jdbc.Driver");

              con=DriverManager.getConnection("jdbc:mysql://localhost/data_buku", "root", "");

               stat=con.createStatement();

               } catch (Exception e) {

                   JOptionPane.showMessageDialog(null, e);

               }

               }

        private void tabel(){

            DefaultTableModel tb= new DefaultTableModel();

            // namain setiap kolum tabel

            tb.addColumn("No.Buku");

            tb.addColumn("Judul Buku");

            tb.addColumn("Pengarang");

            tb.addColumn("Tahun");

            tb.addColumn("Penerbit");

            tabel.setModel(tb);

        try {

            // ambil data dari databasenya

            res=stat.executeQuery ("select * from db_buku");

        while (res.next())

        {

        // tarik data db berdasarkan kolom pada tabel

        // ditampilkan pada tabel Java

        tb.addRow(new Object[]{res.getInt("no_buku"),

        res.getString("judul_buku"),

        res.getString("pengarang"),

        res.getInt("tahun"),

        res.getString("penerbit")

        });

        }

        

        }catch (Exception e) {

        }

        }

    private void kosongkan(){

    no.setText("");

    judul.setText("");

    pengarang.setText("");

    tahun.setText("");

    penerbit.setText("");

    no.requestFocus();

    }

    

    private void ClearTabel(){

    int cb=tabel.getRowCount();

    int cc=tabel.getColumnCount();

    for (int i=0;i                          

    private void initComponents() {



        jLabel1 = new javax.swing.JLabel();

        jLabel2 = new javax.swing.JLabel();

        jLabel3 = new javax.swing.JLabel();

        jLabel4 = new javax.swing.JLabel();

        jLabel5 = new javax.swing.JLabel();

        jLabel6 = new javax.swing.JLabel();

        no = new javax.swing.JTextField();

        judul = new javax.swing.JTextField();

        pengarang = new javax.swing.JTextField();

        tahun = new javax.swing.JTextField();

        penerbit = new javax.swing.JTextField();

        hapus = new javax.swing.JButton();

        simpan = new javax.swing.JButton();

        edit = new javax.swing.JButton();

        keluar = new javax.swing.JButton();

        clear = new javax.swing.JButton();

        baca = new javax.swing.JButton();

        jScrollPane1 = new javax.swing.JScrollPane();

        tabel = new javax.swing.JTable();

        clrdata = new javax.swing.JButton();



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel1.setText("CRUD Database Buku");



        jLabel2.setText("No. Buku");



        jLabel3.setText("Judul");



        jLabel4.setText("Pengarang");



        jLabel5.setText("Tahun");



        jLabel6.setText("Penerbit");



        no.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                noActionPerformed(evt);

            }

        });



        hapus.setText("Hapus");

        hapus.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                hapusActionPerformed(evt);

            }

        });



        simpan.setText("Simpan");

        simpan.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                simpanActionPerformed(evt);

            }

        });



        edit.setText("Edit");

        edit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                editActionPerformed(evt);

            }

        });



        keluar.setText("Keluar");

        keluar.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                keluarActionPerformed(evt);

            }

        });



        clear.setText("Clear Tabel");

        clear.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                clearActionPerformed(evt);

            }

        });



        baca.setText("Baca");

        baca.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                bacaActionPerformed(evt);

            }

        });



        tabel.setModel(new javax.swing.table.DefaultTableModel(

            new Object [][] {

                {null, null, null, null},

                {null, null, null, null},

                {null, null, null, null},

                {null, null, null, null}

            },

            new String [] {

                "Title 1", "Title 2", "Title 3", "Title 4"

            }

        ));

        jScrollPane1.setViewportView(tabel);



        clrdata.setText("Bersihkan");

        clrdata.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                clrdataActionPerformed(evt);

            }

        });



        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addGap(148, 148, 148)

                .addComponent(jLabel1)

                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

                .addGap(38, 38, 38)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                    .addComponent(jLabel2)

                    .addComponent(jLabel3)

                    .addComponent(jLabel4)

                    .addComponent(jLabel5)

                    .addComponent(jLabel6))

                .addGap(10, 10, 10)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                    .addComponent(penerbit)

                    .addComponent(pengarang)

                    .addComponent(judul)

                    .addComponent(no, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addComponent(tahun, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGroup(layout.createSequentialGroup()

                        .addComponent(baca, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                        .addComponent(clear)))

                .addGap(18, 18, 18)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                    .addComponent(clrdata, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)

                    .addComponent(hapus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(edit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(simpan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(keluar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

                .addGap(23, 23, 23))

            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addContainerGap()

                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(17, 17, 17)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jLabel2)

                    .addComponent(no, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addComponent(simpan))

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jLabel3)

                    .addComponent(judul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addComponent(edit))

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jLabel4)

                    .addComponent(pengarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addComponent(hapus))

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jLabel5)

                    .addComponent(tahun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addComponent(clrdata))

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(jLabel6)

                    .addComponent(penerbit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

                .addGap(18, 18, 18)

                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                    .addComponent(keluar)

                    .addComponent(baca)

                    .addComponent(clear))

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))

        );



        pack();

    }//                         



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

        // TODO add your handling code here:

    try {

         // search by primary key

     res = stat.executeQuery("select * from db_buku where "+ "no_buku='"+ no.getText()+"'"

         );

    while (res.next()){

        judul.setText(res.getString("judul_buku"));

        pengarang.setText(res.getString("pengarang"));

        tahun.setText(res.getString("tahun"));

        penerbit.setText(res.getString("penerbit"));        

    }

    } catch (Exception e){

        JOptionPane.showMessageDialog(rootPane,e );

    }   

    }                                  



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

        // TODO add your handling code here:

        try {

            stat.executeUpdate ("insert into db_buku values ("

                    + "'" + no.getText() + "'," 

                    + "'" + judul.getText() + "',"

                    + "'" + pengarang.getText() + "',"

                    + "'" + tahun.getText() + "',"

                    + "'" + penerbit.getText() + "')"

            );

            

            kosongkan();

            tabel();

            JOptionPane.showMessageDialog(null, "Data Tersebut Berhasil Disimpan.");

            } catch (Exception e) {

            JOptionPane.showMessageDialog(null, "Error :"+e);

            }

    }                                      



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

        // TODO add your handling code here:

        tabel();

    }                                    



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

        // TODO add your handling code here:

        try {

            stat.executeUpdate("update db_buku set "

            + "judul_buku='"+judul.getText()+"',"

            + "pengarang='"+pengarang.getText()+"',"

            + "tahun='"+tahun.getText()+"',"

            + "penerbit='"+penerbit.getText()+"'"

            + " where "

            + "no_buku='"+no.getText()+"'"

            );

            

            kosongkan();

            tabel();

            JOptionPane.showMessageDialog(rootPane, "Data Updated");

            }catch (Exception e) {

            JOptionPane.showMessageDialog(rootPane, e);    

            }   

    }                                    



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

        // TODO add your handling code here:

        ClearTabel();

    }                                     



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

        // TODO add your handling code here:

        try {

            stat.executeUpdate("delete from db_buku where "

            + "no_buku='"+no.getText()+"'"

            );

            kosongkan();

            tabel();

            JOptionPane.showMessageDialog(null, "Data Telah Dihapus.");

            }catch (Exception e) {

            JOptionPane.showMessageDialog(null, "Kesalahan : "+e);

            }

    }                                     



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

        // TODO add your handling code here:

        System.exit(0);

    }                                      



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

        // TODO add your handling code here:

        kosongkan ();

    }                                       





No comments:

Post a Comment

What's on your mind