Tutorial Programming

TUTORIAL JAVA SWING CRUD MYSQL



Hallo sahabat saya akan menjelaskan tentang bagaimana CRUD (Create, Read, Update, Delete) pada java swing dengan mengunakan database MYSQL , yang udah pada tau tentang MYSQL bakal gak kesusahan kok, dan yang udah tau konsep perangakat lunak dekstop pasti udah tau juga, baiklah tools yang kita perlukan pertama adalah :

- MYSQL
- NETBEANS
- MYSQL JDBS DRIVER 

Petama-tama buatlah form seperti gambar berikut:



Kedua copy coding berikut: 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

/**
 *
 * @author Ary
 */
 
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Crud extends javax.swing.JFrame {

 private static Connection config; //Koneksi
    private DefaultTableModel model; //Model Tabel
 
 /**
     * Creates new form Crud
     */
    public Crud() {
        initComponents();
        //setExtendedState(MAXIMIZED_BOTH);
        
        //buat model tabel / header tabel
        model=new DefaultTableModel();
        this.jTable1.setModel(model); 
        model.addColumn("ID");
        model.addColumn("Username");
        model.addColumn("Password");
        model.addColumn("Nama Lengkap");
        
        ambil_data_tabel();
    }
    
 //Koneksi
    private static Connection buka_koneksi() {
        if (config==null) {
            try {
                String url="jdbc:mysql://localhost:3306/absen"; //nama database belajar
                String user="root"; //user mysql
                String password=""; //password mysql
               
                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                config=DriverManager.getConnection(url,user,password);
            }catch (SQLException t) {
                System.out.println("Error membuat koneksi");
            }
        }
     return config;
    }
 
 //untuk update nya
 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Connection c=buka_koneksi();

        String sqlkode="Update tbl_admin SET "
            + "username='"+this.jTextField2.getText()+"',"
            + "password='"+this.jTextField3.getText()+"',"
            + "nama='"+this.jTextField4.getText()+"' "
            + "Where id_admin='"+this.jTextField1.getText()+"'";
        
            try {
               PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlkode);
               p2.executeUpdate();
               p2.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getMessage());
            }
        //Action Button Refresh
        JOptionPane.showMessageDialog(null, "Update sukses");
        ambil_data_tabel();
        clear_text();
    } 
 
 //Bagian insert
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        Connection c=buka_koneksi();
        //bikin sql query tambah data
        String sqlkode="Insert into tbl_admin (username,password,nama) "
                + "values ('"+this.jTextField2.getText()+"',"
                + "'"+this.jTextField3.getText()+"',"
                + "'"+this.jTextField4.getText()+"')";
            try {
               PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlkode);
               p2.executeUpdate();
               p2.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getMessage());
            }
        //Action Button Refresh
        JOptionPane.showMessageDialog(null, "Insert sukses");
        ambil_data_tabel();
        clear_text();
    }                                        
 
 
 Bagian delete
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Connection c=buka_koneksi();
         String sqlkode="Delete from tbl_admin "
                 + "Where id_admin='"+this.jTextField1.getText()+"'";
            try {
               PreparedStatement p2=(PreparedStatement) c.prepareStatement(sqlkode);
               p2.executeUpdate();
               p2.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Terjadi kesalahan "+ex.getMessage());
            }
        //Action Button Refresh
        JOptionPane.showMessageDialog(null, "Delete sukses");
        ambil_data_tabel();
        clear_text();
    } 
 
 //Klik dari table
 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        ambil_tabel_klik();
    }                                    
 
 
 //Untuk refresh
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        ambil_data_tabel();
        clear_text();
        // TODO add your handling code here:
    } 
 
 public void ambil_data_tabel(){
        model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            try {            
                Connection c=buka_koneksi();
                Statement s= c.createStatement();
                String sql="Select * from tbl_admin";
                ResultSet r=s.executeQuery(sql);

                while (r.next()) {
                    Object[] o=new Object[4];
                    o[0]=r.getString("id_admin");
                    o[1]=r.getString("username");
                    o[2]=r.getString("password");
                    o[3]=r.getString("nama");


                    model.addRow(o);
                }
                r.close();
                s.close();
                ambil_tabel_klik();
            }catch(SQLException e) {
                System.out.println("Terjadi kesalahan "+e.getMessage());
            }
    }
    
    private void clear_text(){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
    }
    
    private void ambil_tabel_klik(){
        int i=this.jTable1.getSelectedRow();

            if(i==-1)
            {
                return;
            }
            String kode=(String) model.getValueAt(i, 0);
            this.jTextField1.setText(kode);
            String username=(String) model.getValueAt(i, 1);
            this.jTextField2.setText(username);
            String password=(String) model.getValueAt(i, 2);        
            this.jTextField3.setText(password);
            String nama=(String) model.getValueAt(i, 3);
            this.jTextField4.setText(nama);
    }
 
}







Silakan download sample disini.


Selesai tutorial saya semoga bermanfaat yah. 






No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.