Cara membuat CRUD GUI dengan Java



Pada artikel sebelumnya saya sudah membahas tentang JDBC dan sekarang saya akan membahas tentang CRUD dengan Java. Ok sekarang kita sudah mencapai materi terakhir dalam matakuliah PBO. CRUD adalah singkatan dari Create Read Update Delete , yang sering digunakan pada aplikasi-aplikasi pengolahan data yang kebanyakan mengguanakan fungsi CRUD didalamnya . Fungsi ini digunakan untuk menambahkan data, menghapus data, serta mengupdate data.

Apa saja yang dibutuhkan untuk membuat sebuah CRUD GUI pada Java ?
  1. JDBC
  2. MySQL
  3. JFrame

Langkah pertama membuat CRUD
siapkan dulu databasenya, disini saya menggunakan nama database db_mahasiswa dan nama tabelnya mahasiswa.

Buat package baru pada java
Tambahkan library MySQL JDBC Driver
Buat Class koneksi.java
berikut kodenya:

import java.sql.*;

public class koneksi {
    public Connection cc;
    public Statement ss;
    public ResultSet rr;
            
public void Class (){
    
    try {
        Class.forName("com.mysql.jdbc.Driver");
        cc=DriverManager.getConnection("jdbc:mysql://localhost/db_mahasiswa","root","");
        System.out.println("koneksi okey");
        } catch (Exception e){
            System.out.println(e);
            
    }
}  
}


Pindah ke tab Source dan masukan kode berikut:
package crud_tugas;
import java.sql.*;
import java.text.SimpleDateFormat;
import crud_tugas.koneksi;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class CrudMahasiswa extends javax.swing.JFrame {
    koneksi con;
    private Object [][]tbl_input = null;
    private String []label = {"NIM", "NAMA", "PRODI", "ALAMAT", "NO HP"};
        
    public CrudMahasiswa() {
        initComponents();
        con = new koneksi();
        con.Class();
        BacaTabel();        
    }
    
    private void BacaTabel(){
        try {
            con.ss = (Statement) con.cc.createStatement();
            String sql = "Select * From mahasiswa Order By nim";
            con.rr = con.ss.executeQuery(sql);
            ResultSetMetaData m = con.rr.getMetaData();
            int kolom = m.getColumnCount();
            int baris = 0;
            while (con.rr.next()){
                baris = con.rr.getRow();
            }
            tbl_input = new Object[baris][kolom];
            int x = 0;
            con.rr.beforeFirst();            
            while (con.rr.next()){
                tbl_input [x][0] = con.rr.getString("nim");
                tbl_input [x][1] = con.rr.getString("nama");
                tbl_input [x][2] = con.rr.getString("prodi");
                tbl_input [x][3] = con.rr.getString("alamat");
                tbl_input [x][4] = con.rr.getString("no_hp");
                x++;
            }
            txttabel.setModel(new DefaultTableModel(tbl_input, label));                    
        } catch (SQLException e) {    
            JOptionPane.showMessageDialog(null, e);
        }
    }
    
    private void setTabel(){
        int row = txttabel.getSelectedRow();
        txtnim.setText((String)txttabel.getValueAt(row, 0));
        txtnama.setText((String)txttabel.getValueAt(row, 1));        
        txtprodi.setText((String)txttabel.getValueAt(row, 2)); ;
        txtalamat.setText((String)txttabel.getValueAt(row, 3));
        txtnohp.setText((String)txttabel.getValueAt(row, 4));
    }
    
    private void simpan(){
        String nim = this.txtnim.getText();
        String nama = this.txtnama.getText();       
        String prodi = this.txtprodi.getText();
        String alamat = this.txtalamat.getText();
        String no_hp = this.txtnohp.getText();
        try {
          String sql= "Insert into mahasiswa values (?,?,?,?,?)";
          PreparedStatement p = (PreparedStatement) con.cc.prepareStatement(sql);
            p.setString(1, nim);
            p.setString(2, nama);
            p.setString(3, prodi);
            p.setString(4, alamat);
            p.setString(5, no_hp);
            p.executeUpdate();
            
            BacaTabel();
            JOptionPane.showMessageDialog(this, "Data sukses di input");
                        
        }catch (SQLException e){
            System.out.println(e);
        }
    }
    
    private void edit(){
        String nim = this.txtnim.getText();
        String nama = this.txtnama.getText();
        String prodi = this.txtprodi.getText();
        String alamat = this.txtalamat.getText();
        String no_hp = this.txtnohp.getText();
        
        try {
          String sql= "Update mahasiswa Set nama=?, prodi=?, alamat=?, no_hp=? Where nim=?";
          PreparedStatement p = (PreparedStatement) con.cc.prepareStatement(sql);
            p.setString(5, nim);
            p.setString(1, nama);
            p.setString(2, prodi);
            p.setString(3, alamat);
            p.setString(4, no_hp);
            p.executeUpdate();
            
            BacaTabel();
            JOptionPane.showMessageDialog(this, "Data sukses di edit");
                        
        }catch (SQLException e){
            System.out.println(e);
        }
    }
    
    private void hapus(){
        try {
            String sql = "Delete From mahasiswa Where nim = '"+txtnim.getText()+"'";
            con.ss.executeUpdate(sql);
            con.ss.close();
            JOptionPane.showMessageDialog(null, " Data berhasil dihapus");
            BacaTabel();
            txtnim.requestFocus();
                       
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
   
    private  void baru (){
        txtnim.setText("");
        txtnama.setText("");
        txtprodi.setText("");
        txtalamat.setText(""); 
        txtnohp.setText("");
    }
    
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txttabel = new javax.swing.JTable();
        txtnama = new javax.swing.JTextField();
        txtnim = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtalamat = new javax.swing.JTextArea();
        Fsimpan = new javax.swing.JButton();
        Fedit = new javax.swing.JButton();
        Fhapus = new javax.swing.JButton();
        Fbaru = new javax.swing.JButton();
        Fkeluar = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtprodi = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtnohp = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("NAMA");

        jLabel2.setText("NIM");

        jLabel4.setText("DATA MAHASISWA");

        jLabel6.setText("ALAMAT");

        txttabel.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"
            }
        ));
        txttabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txttabelMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(txttabel);

        txtalamat.setColumns(20);
        txtalamat.setRows(5);
        jScrollPane2.setViewportView(txtalamat);

        Fsimpan.setText("SIMPAN");
        Fsimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FsimpanActionPerformed(evt);
            }
        });

        Fedit.setText("EDIT");
        Fedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FeditActionPerformed(evt);
            }
        });

        Fhapus.setText("HAPUS");
        Fhapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FhapusActionPerformed(evt);
            }
        });

        Fbaru.setText("BARU");
        Fbaru.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FbaruActionPerformed(evt);
            }
        });

        Fkeluar.setText("KELUAR");
        Fkeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FkeluarActionPerformed(evt);
            }
        });

        jLabel7.setText("PRODI");

        jLabel8.setText("NO HP");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(29, 29, 29)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(54, 54, 54)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel6)
                                .addComponent(jLabel2)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8))
                            .addGap(69, 69, 69)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtnohp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(Fsimpan, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Fedit, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Fhapus)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Fbaru)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Fkeluar))
                                            .addComponent(txtnim, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(txtprodi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                                                .addComponent(txtnama, javax.swing.GroupLayout.Alignment.LEADING)))
                                        .addGap(0, 0, Short.MAX_VALUE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(241, 241, 241)
                        .addComponent(jLabel4)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(txtnim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtprodi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnohp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Fsimpan)
                    .addComponent(Fedit)
                    .addComponent(Fhapus)
                    .addComponent(Fbaru)
                    .addComponent(Fkeluar))
                .addContainerGap(57, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }//                         

    private void FkeluarActionPerformed(java.awt.event.ActionEvent evt) {                                        
        dispose();
    }                                       

    private void FbaruActionPerformed(java.awt.event.ActionEvent evt) {                                      
        baru();
    }                                     

    private void FhapusActionPerformed(java.awt.event.ActionEvent evt) {                                       
        hapus();
    }                                      

    private void FeditActionPerformed(java.awt.event.ActionEvent evt) {                                      
        edit();
    }                                     

    private void FsimpanActionPerformed(java.awt.event.ActionEvent evt) {                                        
        simpan();
    }                                       

    private void txttabelMouseClicked(java.awt.event.MouseEvent evt) {                                      
        setTabel();
    }                                     

    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CrudMahasiswa().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Fbaru;
    private javax.swing.JButton Fedit;
    private javax.swing.JButton Fhapus;
    private javax.swing.JButton Fkeluar;
    private javax.swing.JButton Fsimpan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea txtalamat;
    private javax.swing.JTextField txtnama;
    private javax.swing.JTextField txtnim;
    private javax.swing.JTextField txtnohp;
    private javax.swing.JTextField txtprodi;
    private javax.swing.JTable txttabel;
    // End of variables declaration                   
}

Setelah itu jalankan projectnya Run File, dan berikut ini hasilya:

crud java gui mysql
Crud Java Gui Mysql
Downlod project diatas pada link di bawah ini:




Keyword:
  • download aplikasi java gui mysql
  • crud java gui mysql
  • download crud gui jframe
  • mysql
  • aplikasi crud java
  • contoh crud mahasiswa dengan java
  • aplikasi tugas besar java
  • source code java gui mysql
  • cara membuat crud dengan java
  • contoh membuat crud dengan java
  • crud mahasiswa dengan java
  • crud mahasiswa
  • program crud java
  • java netbeans
  • matakuliah pbo
  • aplikasi java
  • aplikasi crud java

0 Response to "Cara membuat CRUD GUI dengan Java"

Posting Komentar