Aplikasi SPBU dengan Java Netbeans

.

Pada perjumpaan kali ini Whitecyber team kembali mendapat order dari customer tentang pembuatan Aplikasi SPBU dengan menggunakan program Java Netbeans dan Database Mysql.

Untuk membuat form transaksi SPBU menggunakan Netbeans, Anda dapat mengikuti langkah-langkah berikut:

  1. Buka aplikasi Netbeans dan buat proyek baru dengan nama “Aplikasi SPBU”.
  2. Pada jendela proyek, klik kanan pada folder “Source Packages” dan pilih “New” > “Java Class”. Beri nama kelas “Transaksi” dan klik “Finish”.
  3. Pada editor kode, tulis atribut-atribut yang dibutuhkan untuk kelas Transaksi, seperti nomor transaksi, jenis bahan bakar, jumlah liter, harga per liter, dan total bayar. Juga tulis konstruktor, getter, setter, dan metode toString untuk kelas Transaksi.
  4. Klik kanan pada folder “Source Packages” lagi dan pilih “New” > “JFrame Form”. Beri nama form “FormTransaksi” dan klik “Finish”.
  5. Pada jendela desain, tambahkan komponen-komponen yang dibutuhkan untuk form transaksi, seperti label, text field, combo box, button, dan table. Sesuaikan ukuran, posisi, dan nama variabel dari komponen-komponen tersebut sesuai kebutuhan.
  6. Pada jendela properti, atur nilai-nilai yang sesuai untuk komponen-komponen tersebut, seperti teks, font, warna, dan model tabel. Juga atur event handler untuk komponen-komponen yang memerlukan aksi, seperti button dan combo box.
  7. Pada editor kode, tulis logika program yang dibutuhkan untuk form transaksi, seperti membuat objek Transaksi, menambahkan data ke tabel, menghitung total bayar, dan menyimpan data ke file.
  8. Jalankan program dan coba masukkan beberapa data transaksi untuk melihat hasilnya.

Anda dapat melihat beberapa video tutorial yang saya temukan dari hasil pencarian web saya untuk referensi lebih lanjut.

Projects :

.

Source Code :

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package Koneksi;

/**
 *
 * @author GK3v
 */
import java.sql.*;
import javax.swing.JOptionPane;

public class KoneksiDatabase {
    Connection cn;
    public static Connection BukaKoneksi() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/spbu", "root", "");
            return cn;  
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    } 
}

Source Code

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package form;

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

public class FrmData extends javax.swing.JFrame {

    public Statement st;
    public ResultSet rs;
    Connection cn = Koneksi.KoneksiDatabase.BukaKoneksi();
    int jawab;
    
    public FrmData() {
        initComponents();
        TampilData();
        TampilDataPembelian();
        TampilPendapatan();
    }

    private void Bersih(){
        txtKode.setText("");
        txtProduk.setText("");
        txtHarga.setText("");
        txtStok.setText("");
        
        btnSimpan.setText("Simpan");    
        txtKode.setEditable(true);
    }
    
    private void BersihPembelian(){
        label1.setText("-");
        textField2.setText("-");
        textField1.setText("");
        textField3.setText("");
        textField4.setText("");
        textField5.setText("");
    }
    
    private void CariData(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM fuel WHERE " + jComboBox1.getSelectedItem() + " LIKE '%" + textField6.getText() + "%'" );
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("Produk");
            model.addColumn("Harga");
            model.addColumn("Stok");
            
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);
            
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("Produk"),
                    rs.getString("Harga"),
                    rs.getString("Stok")
                };
                model.addRow(data);
                tblData.setModel(model);
            }
           
        } catch (Exception e) {
            
        }
    }
    
    private void CariFuelStation1(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM fuel WHERE " + jComboBox1.getSelectedItem() + " LIKE '%" + jTextField3.getText() + "%'" );
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("Produk");
            model.addColumn("Harga");
            model.addColumn("Stok");
                       
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);            
        
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("Produk"),
                    rs.getString("Harga"),
                    rs.getString("Stok")
                };
                model.addRow(data);
                tblData.setModel(model);
            }
           
        } catch (Exception e) {
            
        }
    }
    
    private void CariFuelStation2(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM fuel WHERE " + jComboBox2.getSelectedItem() + " LIKE '%" + jTextField6.getText() + "%'" );
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("Produk");
            model.addColumn("Harga");
            model.addColumn("Stok");
                       
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);            
        
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("Produk"),
                    rs.getString("Harga"),
                    rs.getString("Stok")
                };
                model.addRow(data);
                tblData.setModel(model);
            }
           
        } catch (Exception e) {
            
        }
    }
    
    private void CariFuelStation3(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM fuel WHERE " + jComboBox3.getSelectedItem() + " LIKE '%" + jTextField11.getText() + "%'" );
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("Produk");
            model.addColumn("Harga");
            model.addColumn("Stok");
                       
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);            
        
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("Produk"),
                    rs.getString("Harga"),
                    rs.getString("Stok")
                };
                model.addRow(data);
                tblData.setModel(model);
            }
           
        } catch (Exception e) {
            
        }
    }
    
    
    private void TampilData(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM fuel");
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("Produk");
            model.addColumn("Harga");
            model.addColumn("Stok");
            
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);
            
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("Produk"),
                    rs.getString("Harga"),
                    rs.getString("Stok")
                };
                model.addRow(data);
                tblData.setModel(model);
            }
           
        } catch (Exception e) {
            
        }
    }
    
    
private void TampilDataPembelian(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT * FROM pembelian");
                    
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Kode");
            model.addColumn("JumlahIsi");
            model.addColumn("TotalHarga");
            model.addColumn("Bayar");
            model.addColumn("Kembalian");
            
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);
            
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("Kode"),
                    rs.getString("JumlahIsi"),
                    rs.getString("TotalHarga"),
                    rs.getString("Bayar"),
                    rs.getString("Kembalian")
                };
                model.addRow(data);
                tblDataPembelian.setModel(model);
                //textField7.getText(tot);
            }
           
        } catch (Exception e) {
            
        }
    }

private void TampilPendapatan(){
        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT SUM(TotalHarga) AS total FROM pembelian");
                    
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("No.");
            model.addColumn("Total Pendapatan");
            
            int no = 1;
                        
            model.getDataVector().removeAllElements();
            model.fireTableDataChanged();
            model.setRowCount(0);
            
            while (rs.next()) {
                Object[] data = {
                    no ++,
                    rs.getString("total")
                };
                model.addRow(data);
                tblDataPendapatan.setModel(model);
                
            }
           
        } catch (Exception e) {
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        panel1 = new java.awt.Panel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtKode = new javax.swing.JTextField();
        txtProduk = new javax.swing.JTextField();
        txtHarga = new javax.swing.JTextField();
        txtStok = new javax.swing.JTextField();
        btnSimpan = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        btnBatal = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        button1 = new java.awt.Button();
        textField1 = new java.awt.TextField();
        textField2 = new java.awt.TextField();
        textField3 = new java.awt.TextField();
        button2 = new java.awt.Button();
        textField4 = new java.awt.TextField();
        textField5 = new java.awt.TextField();
        label1 = new java.awt.Label();
        jButton1 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        textField6 = new java.awt.TextField();
        button3 = new java.awt.Button();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblDataPembelian = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDataPendapatan = new javax.swing.JTable();

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Kode");

        jLabel2.setText("Produk");

        jLabel3.setText("Harga");

        jLabel4.setText("Stok");

        txtKode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtKodeActionPerformed(evt);
            }
        });

        btnSimpan.setText("Simpan");
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        btnBatal.setText("Batal");
        btnBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatalActionPerformed(evt);
            }
        });

        tblData.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 [] {
                "Kode", "Produk", "Harga", "Stok"
            }
        ));
        tblData.setName(""); // NOI18N
        tblData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDataMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblData);

        jLabel6.setText("Liter");

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        jLabel7.setText("Jumlah Pengisian");

        jLabel9.setText("Pembayaran");

        jLabel15.setText("Liter");

        button1.setLabel("Total");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        textField2.setBackground(new java.awt.Color(51, 0, 153));
        textField2.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        textField2.setForeground(new java.awt.Color(255, 255, 255));
        textField2.setText("-");

        button2.setLabel("Kembalian");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        label1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        label1.setForeground(new java.awt.Color(51, 0, 153));
        label1.setText("-");

        jButton1.setText("Simpan Transaksi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textField5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(textField4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addGap(10, 10, 10)))
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel15)
                                        .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(34, 34, 34)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(20, 20, 20)
                                .addComponent(textField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel9))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel15))
                                .addGap(80, 80, 80)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(44, 44, 44))
        );

        jTabbedPane1.addTab("FUEL STATION", new javax.swing.ImageIcon(getClass().getResource("/gambar/fuel station.jpg")), jPanel1); // NOI18N

        jLabel8.setText("Cari Jenis Bahan Bakar");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Kode", "Produk" }));

        button3.setLabel("Cari");
        button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button3ActionPerformed(evt);
            }
        });

        tblDataPembelian.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Kode", "Jumlah Isi", "TotalHarga", "Bayar", "Kembalian"
            }
        ));
        jScrollPane3.setViewportView(tblDataPembelian);

        tblDataPendapatan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Total Pendapatan"
            }
        ));
        jScrollPane2.setViewportView(tblDataPendapatan);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtProduk, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(btnSimpan)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnHapus)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnBatal))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(txtStok, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel6))
                                .addComponent(txtKode, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtHarga)))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField6, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtKode, 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(jLabel2)
                            .addComponent(txtProduk, 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(jLabel3)
                            .addComponent(txtHarga, 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(jLabel4)
                            .addComponent(txtStok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSimpan)
                            .addComponent(btnHapus)
                            .addComponent(btnBatal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(textField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(14, 14, 14))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        try {
            st = cn.createStatement();
            if (txtKode.getText().equals("") || txtProduk.getText().equals("") || txtHarga.getText().equals("") || txtStok.getText().equals("") ){
               JOptionPane.showMessageDialog(null, "Data Tidak Boleh Kosong", "Validasi Data", JOptionPane.INFORMATION_MESSAGE);
               return;
            }
            
            if (btnSimpan.getText() == "Simpan") {
                String cek = "SELECT * FROM fuel WHERE Kode = '" + txtKode.getText() + "'";
                rs = st.executeQuery(cek);
                if ( rs.next()) {
                    JOptionPane.showMessageDialog(null, "Ups... Kode ini sudah ada");
                } else {
                    String sql = "INSERT INTO fuel VALUES ('" + txtKode.getText() + "','" + txtProduk.getText() + "','" + txtHarga.getText() + "','" + txtStok.getText() + "')";
                    st.executeUpdate(sql);
                    JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
                    Bersih();
                    TampilData();
                }
            } else {
                    // aksi ubah data
                    String update = "UPDATE fuel SET Produk = '" + txtProduk.getText() + "', Harga = '" + txtHarga.getText() + "', Stok = '" + txtStok.getText() + "' WHERE Kode = '" + txtKode.getText() + "'";
                    st.executeUpdate(update);
                    JOptionPane.showMessageDialog(null, "Data Berhasil Diupdate");
                    Bersih();
                    TampilData();
            }
            
        } catch (Exception e) {
            
        }
    }                                         

    private void tblDataMouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        txtKode.setText(tblData.getValueAt(tblData.getSelectedRow(), 1).toString());
        txtProduk.setText(tblData.getValueAt(tblData.getSelectedRow(), 2).toString());
        txtHarga.setText(tblData.getValueAt(tblData.getSelectedRow(), 3).toString());
        txtStok.setText(tblData.getValueAt(tblData.getSelectedRow(), 4).toString());
        
        textField2.setText(tblData.getValueAt(tblData.getSelectedRow(), 3).toString());
        label1.setText(tblData.getValueAt(tblData.getSelectedRow(), 2).toString());
                       
        txtKode.setEditable(false);
        btnSimpan.setText("Ubah");
    }                                    

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (txtKode.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Silahkan Pilih Data yang mau dihapus !");
        } else {
            int jawab = JOptionPane.showConfirmDialog(null, "Data ini akan dihapus, lanjutkan ?", "Konfirmasi", JOptionPane.YES_NO_OPTION);
        }   if (jawab == 0) {
            try {
                st = cn.createStatement();
                String sql = "DELETE FROM fuel WHERE Kode = '" + txtKode.getText() + "'";
                st.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus");
                TampilData();
                Bersih();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,e);
            }
        } 
    }                                        

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

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

    private void button3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        CariData();
    //ketik disini// TODO add your handling code here:
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            st = cn.createStatement();
            if (textField1.getText().equals("") || textField2.getText().equals("") || textField3.getText().equals("") || textField4.getText().equals("") ){
                JOptionPane.showMessageDialog(null, "Data Tidak Boleh Kosong", "Validasi Data", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            if (jButton1.getText() == "Simpan Transaksi") {
                String cek = "SELECT * FROM pembelian WHERE Kode = '" + txtKode.getText() + "'";
                rs = st.executeQuery(cek);
                if ( rs.next()) {
                    JOptionPane.showMessageDialog(null, "Ups... Kode ini sudah ada");
                } else {
                    String sql = "INSERT INTO pembelian VALUES ('" + txtKode.getText() + "','" + textField1.getText() + "','" + textField3.getText() + "','" + textField4.getText() + "','" + textField5.getText() + "')";
                    st.executeUpdate(sql);
                    JOptionPane.showMessageDialog(null, "Data Berhasil Disimpan");
                    BersihPembelian();
                    TampilDataPembelian();
                    TampilPendapatan();
                }
            } else {
                // aksi ubah data
                String update = "UPDATE fuel SET Produk = '" + txtProduk.getText() + "', Harga = '" + txtHarga.getText() + "', Stok = '" + txtStok.getText() + "' WHERE Kode = '" + txtKode.getText() + "'";
                st.executeUpdate(update);
                JOptionPane.showMessageDialog(null, "Data Berhasil Diupdate");
                BersihPembelian();
                TampilData();
            }

        } catch (Exception e) {

        }
        // TODO add your handling code here:
    }                                        

    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        //create variable
        int angka4,angka5,hasil2;

        //input nilai
        angka4 = Integer.parseInt(textField3.getText());
        angka5 = Integer.parseInt(textField4.getText());

        //ru;mus
        hasil2 = angka5 - angka4;

        //output
        textField5.setText(Integer.toString(hasil2));// TODO add your handling code here:        // TODO add your handling code here:
    }                                       

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        //create variable
        int angka1,angka2,hasil;

        //input nilai
        angka1 = Integer.parseInt(textField1.getText());
        angka2 = Integer.parseInt(textField2.getText());

        //ru;mus
        hasil = angka1 * angka2;

        //output
        textField3.setText(Integer.toString(hasil));// TODO add your handling code here:
    }                                       

    /**
     * @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(FrmData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmData.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 FrmData().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnBatal;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Button button3;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private java.awt.Label label1;
    private java.awt.Panel panel1;
    private javax.swing.JTable tblData;
    private javax.swing.JTable tblDataPembelian;
    private javax.swing.JTable tblDataPendapatan;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    private java.awt.TextField textField3;
    private java.awt.TextField textField4;
    private java.awt.TextField textField5;
    private java.awt.TextField textField6;
    private javax.swing.JTextField txtHarga;
    private javax.swing.JTextField txtKode;
    private javax.swing.JTextField txtProduk;
    private javax.swing.JTextField txtStok;
    // End of variables declaration                   
}

Leave a Reply

Your email address will not be published. Required fields are marked *