Abdurrahman Aqil

My notes~

Monday, August 12, 2019

Program Java Netbeans : Penjualan


Membuat Progam Penjualan
A.      Membuat Database
1.       Buka & jalankan Xampp, klik Admin pada Mysql atau buka phpMyAdmin di browser.
2.       Buat database dengan nama ‘penjualan’.
3.       Buat table dengan nama ‘barang’ isi dengan 6 kolom :
Name
Type
Length/ Values
kd_brg
Varchar
12
nm_brg
Varchar
25
satuan
Varchar
20
harga
Int
11
stok
Int
11
stok_min
Int
11
Jadikan kd_brg sebagai primary key.
Perhatikan besar-kecilnya huruf.
B.      Membuat koneksi Java-Mysql
1.       Buka NetBeans, buat package baru bernama Penjualan.
2.       Buat Java Class pada package Penjualan, beri nama ‘KoneksiMysql’ :
import com.mysql.jdbc.Driver;
import java.sql.*;

public class KoneksiMysql {
    public static Connection koneksi;
        public static Connection getConnection () throws SQLException{
           if(koneksi == null){
               new Driver();
               koneksi = DriverManager.getConnection("jdbc:mysql://localhost:3306/penjualan","root","");
           }
            return koneksi;
        }
        public static void main(String []args){
            try{
                getConnection();
                System.out.println("Koneksi berhasil");
            }catch (SQLException ex){
                System.out.println("Koneksi gagal");
            }
        }
}

3. Klik kanan pada library di bawah nama Package
4. Pilih Add Library,
5. Pilih MySQL JDBC Driver, klik Add Library



C.      Membuat Design UI
1.       Buat JFrame Form pada package Penjualan, beri nama ‘frmbarang’.
2.       Buat tampilan seperti di bawah ini.
3.     Ubah nama default TextField, Button, ComboBox & Table

Pallete
Nama
jTextField1
jTxtkd_brg
jTetxtField2
jTxtnm_brg
jComboBox1
jCmbsatuan
jTextField3
jTxtharga
jTextField4
jTxtStok
jTextField5
jTxtStokMin
jButton1
jBtnTambah
jButton2
jBtnSimpan
jButton3
jBtnKoreksi
jButton4
jBtnHapus
jButton5
jBtnBatal
jButton6
jBtnKeluar
jTable1
tblBrg

D.      Source Code
1.     Import & Class frmbarang
Package Penjualan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class frmbarang extends javax.swing.JFrame {

Connection Con;
ResultSet RsBrg;
Statement stm;

Boolean ada = false;
String sSatuan;
Boolean bay = false;
Boolean bo = false;
private Object[][] dataTable = null;
private String [] header = {"Kode","Nama barang","Satuan","Harga","Stok","Stok Min"};
    private int Integer;
    private Connection con;
    private Object konek;
    private Object ikat;

    public frmbarang() {
        initComponents();
        open_db();
        baca_data();
        aktif(false);
        setTombol(true);
    }
private void setField(){
    int row = tblBrg.getSelectedRow();
    jTxtkd_brg.setText((String)tblBrg.getValueAt(row,0));
    jTxtnm_brg.setText((String)tblBrg.getValueAt(row,1));
    jCmbsatuan.setSelectedItem((String)tblBrg.getValueAt(row,2));
    String harga = Double.toString((Integer)tblBrg.getValueAt(row,3));
    jTxtHarga.setText(harga);
    String stok = Double.toString((Integer)tblBrg.getValueAt(row,4));
    jTxtStok.setText(stok);
    String stok_min=Double.toString((Integer)tblBrg.getValueAt(row,5));
    jTxtStokMin.setText(stok_min);
}
private void open_db(){
try{
    Con = KoneksiMysql.getConnection();
}catch(Exception e){
    System.out.println("Error:"+e);
}
}
private void baca_data(){
    try{
     stm = Con.createStatement();
     RsBrg=stm.executeQuery("select * from barang");
     ResultSetMetaData meta = RsBrg.getMetaData();
     int col = meta.getColumnCount();
     int baris =0;
     while(RsBrg.next()){
          baris = RsBrg.getRow();
     }
     dataTable = new Object[baris][col];
     int x =0;
     RsBrg.beforeFirst();
     while(RsBrg.next()){
         dataTable[x][0]=RsBrg.getString("kd_brg");
         dataTable[x][1]=RsBrg.getString("nm_brg");
         dataTable[x][2]=RsBrg.getString("satuan");
         dataTable[x][3]=RsBrg.getString("harga");
         dataTable[x][4]=RsBrg.getString("stok");
         dataTable[x][5]=RsBrg.getString("stok_min");
         x++;
     }
     tblBrg.setModel(new DefaultTableModel(dataTable,header));
    }catch(SQLException e){
        JOptionPane.showMessageDialog(null, e);
    }

}
private void kosong(){
    jTxtkd_brg.setText("");
    jTxtnm_brg.setText("");
    jTxtHarga.setText("");
    jTxtStok.setText("");
    jTxtStokMin.setText("");
}
private void aktif(boolean x){
    jTxtkd_brg.setEditable(x);
    jTxtnm_brg.setEditable(x);
    jCmbsatuan.setEditable(x);
    jTxtHarga.setEditable(x);
    jTxtStok.setEditable(x);
    jTxtStokMin.setEditable(x);
   
}
private void setTombol(boolean t){
    jBtnTambah.setEnabled(t);
    jBtnKoreksi.setEnabled(t);
    jBtnHapus.setEnabled(t);
    jBtnBatal.setEnabled(!t);
    jBtnSimpan.setEnabled(!t);
    jBtnKeluar.setEnabled(t);
}
2.     jBtnKeluarActionPerformed
System.exit(0);
3.     jBtnBatalActionPerformed
aktif(true);
setTombol(false);
4.     jBtnHapusActionPerformed
try {
    String sql = "delete from barang where kd_brg='"+jTxtkd_brg.getText()+"'";
    stm.executeUpdate(sql);
    baca_data();
}catch(SQLException e){
    JOptionPane.showMessageDialog(null, e);
   
}
5.     jBtnKoreksiActionPerformed
bo = true;


6.     jBtnKoreksiMouseClicked
aktif(true);
    setTombol(false);
    jTxtkd_brg.setEditable(false);
7.     jBtnSimpanMouseClicked
String tkode=jTxtkd_brg.getText();
String tnama=jTxtnm_brg.getText();
String mSatuan = (String)jCmbsatuan.getSelectedItem();
Double hrg= Double.parseDouble(jTxtHarga.getText());
Double stk = Double.parseDouble(jTxtStok.getText());
Double stkMin=Double.parseDouble(jTxtStokMin.getText());
try{
if(bo==true){
    stm.executeUpdate("update barang set nm_brg='"+tnama+"',satuan ='"+mSatuan+"',harga="+hrg+",stok="+stk+",stok_min="+stkMin+"where kd_brg='"+tkode+"'");
}else{
    stm.executeUpdate("insert into barang values ('"+jTxtkd_brg.getText()+"','"+jTxtnm_brg.getText()+"','"+mSatuan+"','"+jTxtHarga.getText()+"','"+jTxtStok.getText()+"','"+jTxtStokMin.getText()+ "')");
}
tblBrg.setModel(new DefaultTableModel(dataTable,header));
baca_data();
aktif(true);
setTombol(true);
}catch (SQLException e){
    JOptionPane.showMessageDialog(null, e);
}
8.     jBtnTambahMouseClicked
aktif(true);
     setTombol(false);
kosong();
bay = false;

No comments:

Post a Comment