Cara Membuat Form Data Master Barang di Netbeans ( Aplikasi Pembelian barang Part 5 )

1.     Form Data Barang

Buat Form baru dengan nama “form_barang”. Beri nama variable sesuai gambar dibawah ini.   

Gambar dari : mamanmulyadi.blogspot.com


>> Coding

package uts_uas;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class form_barang extends javax.swing.JFrame {
Connection cn=null;
ResultSet rsbrg=null;
DefaultTableModel tabModel;
public form_barang() {
        initComponents();
        setLocationRelativeTo(this);
        koneksi();
        tampilData();
        nonaktif();
        buatnomor();
    }
public void buatnomor(){
        try{
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery("SELECT kode_barang FROM barang ORDER BY kode_barang DESC");
             if (rs.next()) {
                String kd_brg = rs.getString("kode_barang").substring(1);
                String AN = "" + (Integer.parseInt(kd_brg) + 1);
                String Nol = "";
                if(AN.length()==1)
                {Nol = "00";}
                else if(AN.length()==2)
                {Nol = "0";}
                else if(AN.length()==3)
                {Nol = "";}
                tkode.setText("B" + Nol + AN);//sesuaikan dengan variable namenya
            } else {
                tkode.setText("B001");//sesuaikan dengan variable namenya
            }
            rs.close();
        }catch (Exception e){
        }
    }
void bersih(){
        tkode.setText("");
        tnama.setText("");
        cmbjenis.setSelectedItem("pilih");
        thargabeli.setText(null);
        thargajual.setText("");
        tstok.setText("");
       
    }
void nonaktif(){
        bsimpan.setEnabled(false);
        bhapus1.setEnabled(false);
        bupdate1.setEnabled(false);
        tkode.setEnabled(false);
        tnama.setEnabled(false);
        cmbjenis.setEnabled(false);
        thargabeli.setEnabled(false);
        thargajual.setEnabled(false);
        tstok.setEnabled(false);
    }
void aktif(){
       
        tkode.setEnabled(true);
        tnama.setEnabled(true);
        cmbjenis.setEnabled(true);
        thargabeli.setEnabled(true);
        thargajual.setEnabled(true);
        tstok.setEnabled(true);
    }
void koneksi(){
        try{
            //aktif driver
            Class.forName("com.mysql.jdbc.Driver");
            cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_tugas_java","root","");
            Object[] judul_kolom={"Kode","Nama Barang","Jenis","Harga Jual","Harga Beli","Stok"};
            tabModel=new DefaultTableModel(null,judul_kolom);
            jtabelbarang.setModel(tabModel);
            tampilData();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
void tampilData(){
        try{
            Statement stm; //membuat statemen stm
            stm=cn.createStatement();
            tabModel.getDataVector().removeAllElements();
            rsbrg=stm.executeQuery("select * from barang");//mengambil data barang
            while (rsbrg.next()){
                Object[] data={rsbrg.getString("kode_barang"),
                               rsbrg.getString("nama_barang"),
                               rsbrg.getString("jenis"),
                               rsbrg.getString("harga_beli"),
                               rsbrg.getString("harga_jual"),
                               rsbrg.getString("stok")
                                           };
            tabModel.addRow(data);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

// Klik kanan  pada “bsimpan” > Events > Action > actionPerformed[bsimpanActionPerformed]
    private void bsimpanActionPerformed(java.awt.event.ActionEvent evt) {                                       
try{
    Statement stm=cn.createStatement();
    stm.executeUpdate("insert into barang set kode_barang='"+tkode.getText()+"',"+
                      "nama_barang='"+tnama.getText()+"',"+
                      "jenis='"+cmbjenis.getSelectedItem()+"',"+
                      "harga_beli='"+thargabeli.getText()+"',"+
                      "harga_jual='"+thargajual.getText()+"',"+
                      "stok='"+tstok.getText()+"'");
   
    tampilData();
    bersih();
    bsimpan.setEnabled(false);
    btambah.setEnabled(true);
    btambah.requestFocus();
    JOptionPane.showMessageDialog(rootPane, "Data Berhasil Disimpan");
}catch(Exception e){
    e.printStackTrace();
}
  }      


// Klik kanan  pada “btambah” > Events > Action > actionPerformed[bstambahActionPerformed] 
  private void btambahActionPerformed(java.awt.event.ActionEvent evt)
{                                       
bersih();
aktif();
buatnomor();
tkode.setEnabled(false);
btambah.setEnabled(false);
tnama.requestFocus();   }                


// Klik kanan  pada “bupdate1” > Events > Action > actionPerformed[bupdate1ActionPerformed] 
 
  private void bupdate1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
try{
    Statement stm=cn.createStatement();
    stm.executeUpdate("update barang set kode_barang='"+tkode.getText()+"',"+
                       "nama_barang='"+tnama.getText()+"',"+
                       "jenis='"+cmbjenis.getSelectedItem()+"',"+
                       "harga_beli='"+thargabeli.getText()+"',"+
                       "harga_jual='"+thargajual.getText()+"',"+
                       "stok='"+tstok.getText()+"'where kode_barang='"+
                      
                       tabModel.getValueAt(jtabelbarang.getSelectedRow(),0)+"'");
    tampilData();
    JOptionPane.showMessageDialog(rootPane, "Data Berhasil Diperbaharui.");
    bersih();
}catch(Exception e){
    e.printStackTrace();
}  }    


// Klik kanan  pada “bexit” > Events > Action > actionPerformed[bexitActionPerformed] 
 
  private void bexitActionPerformed(java.awt.event.ActionEvent evt) {                                             
        this.dispose();
    }


// Klik kanan  pada “bhapus1” > Events > Action > actionPerformed[bhapus1ActionPerformed] 
  
  private void bhapus1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
try{
    Statement stm=cn.createStatement();
    stm.executeUpdate("delete from barang where kode_barang='"+
            tabModel.getValueAt(jtabelbarang.getSelectedRow(),0)+"'");
    tampilData();
    JOptionPane.showMessageDialog(rootPane, "Data Berhasil Dihapus");
}catch(Exception e){
    e.printStackTrace();
}
   }   


   // Klik kanan pada “bhapus1” > Events > Mouse > MouseClicked [jtabelbarangMouseClicked] 
  
 private void jtabelbarangMouseClicked(java.awt.event.MouseEvent evt)
{                                         
tkode.setText(tabModel.getValueAt(jtabelbarang.getSelectedRow(),0)+"");
tnama.setText(tabModel.getValueAt(jtabelbarang.getSelectedRow(),1)+"");
cmbjenis.setSelectedItem(tabModel.getValueAt(jtabelbarang.getSelectedRow(),2)+"");
thargabeli.setText(tabModel.getValueAt(jtabelbarang.getSelectedRow(),3)+"");
thargajual.setText(tabModel.getValueAt(jtabelbarang.getSelectedRow(),4)+"");
tstok.setText(tabModel.getValueAt(jtabelbarang.getSelectedRow(),5)+"");
bsimpan.setEnabled(false);
bupdate1.setEnabled(true);
bhapus1.setEnabled(true);
  }                                        


   // Klik kanan pada “bcari” > Events > Action > actionPerformed[bcariActionPerformed] 
 
 private void bcariActionPerformed(java.awt.event.ActionEvent evt) {                                     
try{
    Statement stm=cn.createStatement();
    tabModel.getDataVector().removeAllElements();
    rsbrg=stm.executeQuery("select * from barang where nama_barang like '%"+tcari.getText()+"%'");
    while (rsbrg.next()){
        Object[] data={rsbrg.getString("kode_barang"),
                       rsbrg.getString("nama_barang"),
                       rsbrg.getString("jenis"),
                       rsbrg.getString("harga_beli"),
                       rsbrg.getString("harga_jual"),
                       rsbrg.getString("stok")
        };
        tabModel.addRow(data);
    }
}catch(Exception e){
    e.printStackTrace();
}
  }
   // Klik kanan pada “tcari” > Events > Key > KeyPressed [tcariKeyPressed] 
 
    private void tcariKeyPressed(java.awt.event.KeyEvent evt) {                                
if(evt.getKeyCode() == KeyEvent.VK_ENTER){
        try{
    Statement stm=cn.createStatement();
    tabModel.getDataVector().removeAllElements();
    rsbrg=stm.executeQuery("select * from barang where nama_barang like '%"+tcari.getText()+"%'");
    while (rsbrg.next()){
        Object[] data={rsbrg.getString("kode_barang"),
                       rsbrg.getString("nama_barang"),
                       rsbrg.getString("jenis"),
                       rsbrg.getString("harga_beli"),
                       rsbrg.getString("harga_jual"),
                       rsbrg.getString("stok")
        };
        tabModel.addRow(data);
    }
}catch(Exception e){
    e.printStackTrace();
}      }      }   


   // Klik kanan pada “brefresh” > Events > Action > actionPerformed[brefreshActionPerformed] 
 
  
     private void brefreshActionPerformed(java.awt.event.ActionEvent evt) {                                        
      tampilData();  // TODO add your handling code here:
    }

 Klik Kanan pada “cmbjenis” > Properties



Gambar dari : mamanmulyadi.blogspot.com




Gambar dari : mamanmulyadi.blogspot.com



     // Klik kanan pada “cmbjenis” > Events > Action > actionPerformed[cmbjenisActionPerformed] 
  private void cmbjenisActionPerformed(java.awt.event.ActionEvent evt)
{                                        
bsimpan.setEnabled(true);             }
public static void main(String args[]) {
       
Look and feel setting code (optional)      
 /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new form_barang().setVisible(true);
            }
        });
    }