Minggu, 12 Februari 2012

MEMBUAT DATA PENJUALAN DENGAN NET BEANS

         Membuat data penjualan barang dengan menggunakan Net beans dengan Ms.Access data base.
Pertama-tama buat lah data base di Ms.access.kebetulan saya menggunakan Ms.access 2007.
-Buka Ms.Access > blank data base > beri nama, kebetulan ini saya beri nama uasTia. >Simpan dengan format Ms Access 2003 atau tipe Data base *mdb
-Buat lah tabelnya
 1.Tabel user




2.Tabel data Penjualan



3.Tabel Konsumen
 
Tapi sebelum itu pastikan data basenya sudah diaktifkan di Odbc.
-setelah itu bukalah net beans
-New project > java > java application > next > beri nama > finish
-Di source Package > klik kanan > new > java class > Beri nama > finish
(ini merupakan koneksi untuk ke data base).Ketik kodingnya.
package deden_herdiansyah;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
/**
 *
 * @author Darryl
 */
public class koneksi {
void testKoneksi()
{
try
{
String dbname="jdbc:odbc:uasTia";//nama ODBC
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"Admin","admin");
JOptionPane.showMessageDialog(null,"Koneksi Sukses","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);   
}   
}
public static void main (String args[])
{
koneksi test=new koneksi();
test.testKoneksi();
System.exit(0);
}           
}
-Selanjutnya kita akan membuat for untuk loginnya Sourge Package > kik kanan > new > JFrame form > finish...Buatlah designnya
 
Kodingnya
package deden_herdiansyah;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

import java.sql.DriverManager;
/**
 *
 * @author Darryl
 */
public class formlogin extends javax.swing.JFrame {
public Connection con;
public Statement statement;
    /** Creates new form formlogin */
    public formlogin() {
        initComponents();
    }
private void koneksi(){
    try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:uasTia","Admin","admin");
statement = con.createStatement();

}catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
//method untuk login
public void cariuser(){
    try{
        koneksi();
        String sql =  "Select * from user where user_name='"+txtuser.getText()+"' and password='" + txtpassword.getText() + "'";
        statement =   con.createStatement();
        ResultSet RUser = statement.executeQuery(sql);
        if (RUser.next())
        {
            JOptionPane.showMessageDialog(null, "Selamat Datang.....!");
        new form_menu().show();
        this.dispose(); }
        }catch (Exception e){}

        txtuser.setText("");
        txtpassword.setText("");
        
}
private void txtuserKeyReleased(java.awt.event.KeyEvent evt) {                                   
// TODO add your handling code here:
    if (evt.getKeyCode() == 10){
            txtpassword.requestFocus();
        } 
}                                   

private void txtpasswordKeyPressed(java.awt.event.KeyEvent evt) {                                      
// TODO add your handling code here:
     if (evt.getKeyCode() == 10){
            cblogin.doClick();
        }
}                                      

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

private void cbbatalActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
    dispose();
}                                     
-Untuk Form menu. Source package > Klik kanan > new > JFrame form > Beri nama > finish
Designnya


package deden_herdiansyah;

import javax.swing.JOptionPane;

/**
 *
 * @author Darryl
 */
public class form_menu extends javax.swing.JFrame {

    /** Creates new form form_menu */
    public form_menu() {
        initComponents();
    }
private void jCheckBoxMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                                   
// TODO add your handling code here:
    new form_user().setVisible(true);
}                                                 

private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                                   
// TODO add your handling code here:
    new form_penjualan().setVisible(true);
}                                                 

private void jCheckBoxMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
// TODO add your handling code here:
    new form_konsumen().setVisible(true);
}                                                 

private void jCheckBoxMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
// TODO add your handling code here:
    new profil().setVisible(true);
}                                                 

private void jCheckBoxMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
// TODO add your handling code here:
    int x;
    x=JOptionPane.showConfirmDialog(null, "Anda Ingin Keluar Form User Dan Kembali ke Main Menu","Informasi",JOptionPane.YES_NO_OPTION);
    if(x==0)
dispose();
}                             
-Untuk Form Data Penjualan. Source package > Klik kanan > new > JFrame form > Beri nama > finish
Designnya

Kodingnya :

package deden_herdiansyah;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import java.awt.*;
/**
 *
 * @author Darryl
 */
public class form_penjualan extends javax.swing.JFrame {
public Connection con;
public Statement statement;
String tanggal;
private Dimension UM = Toolkit.getDefaultToolkit().getScreenSize();
    /** Creates new form form_penjualan */
    public form_penjualan() {
        initComponents();
        txttgl.setEditor(new JSpinner.DateEditor(txttgl,"yyyy/MM/dd"));
        isicombo();
      
        setLocation(UM.width/2-getWidth()/2,UM.height/2-getHeight()/2);
    }
private void hitung()
{
    int xjumlah, xharga;
    double xtotal;

    xjumlah =Integer.parseInt(txtjumlah.getText());
     xharga =Integer.parseInt(txtharga.getText());
     
    xtotal =  (xjumlah * xharga);

    txtjumlah.setText(String.valueOf(xjumlah));
    txtharga.setText(String.valueOf(xharga));
    txttotal.setText(String.valueOf(xtotal));
   
   }

    private void isicombo() {
        try{
koneksi();
String sql = "Select * from data_penjualan";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
cmbno.addItem(rs.getString(1)); }
} catch (Exception e){} }
private void cari_no_faktur()
{
try{
koneksi();
String sql = "Select * from data_penjualan where no_faktur ='"+ cmbno.getSelectedItem()+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtno_faktur.setText(rs.getString(1)); 
txtkode.setText(rs.getString(2));
txtnama.setText(rs.getString(3));
txtjumlah.setText(rs.getString(4));
txtharga.setText(rs.getString(5));
txttotal.setText(rs.getString(6));
JOptionPane.showMessageDialog(null, "Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Data tidak ditemukan ","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}  


private void koneksi(){
    try {
//String dbname="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:uasTia","Admin","admin");
statement = con.createStatement();
}catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void cmbnoActionPerformed(java.awt.event.ActionEvent evt) {                                     
// TODO add your handling code here:
    cari_no_faktur();
}                                    

private void txthargaKeyPressed(java.awt.event.KeyEvent evt) {                                   
if(evt.getKeyCode() == 10){
    hitung();
    // TODO add your handling code here:
    }            
}                                  

private void txtjumlahKeyReleased(java.awt.event.KeyEvent evt) {                                      
 if(evt.getKeyCode() == 10){
    txtharga.requestFocus();// TODO add your handling code here:
}                                 
   
}                                    

private void btcariActionPerformed(java.awt.event.ActionEvent evt) {                                      
// TODO add your handling code here:
    String no_faktur = txtno_faktur.getText();
try
{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="SELECT * FROM data_penjualan WHERE no_faktur like '"+no_faktur+"'";
 ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtkode.setText(rs.getString(2));
txtnama.setText(rs.getString(3));
txtjumlah.setText(rs.getString(4));
txtharga.setText(rs.getString(5));
txttotal.setText(rs.getString(6));
JOptionPane.showMessageDialog(null, "Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Data tidak ditemukan ","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}   
}                                     

private void btsimpanActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
    String no_faktur = txtno_faktur.getText();
String kode_barang = txtkode.getText();
String nama_barang = txtnama.getText();
String jumlah = txtjumlah.getText();
String harga = txtharga.getText();
String total = txttotal.getText();
try
{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="insert into data_penjualan values('"+no_faktur+"','"+kode_barang+"','"+nama_barang+"','"+jumlah+"','"+harga+"','"+total+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null, "Data berhasil dimasukkan..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
txtno_faktur.setText("");
txtkode.setText("");
txtnama.setText("");
txtjumlah.setText("");
txtharga.setText("");
txttotal.setText("");
//txtno_faktur.setFocusable(rootPaneCheckingEnabled);
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}                                       

private void btubahActionPerformed(java.awt.event.ActionEvent evt) {                                      
// TODO add your handling code here:
    String no_faktur = txtno_faktur.getText();
String kode_barang = txtkode.getText();
String nama_barang = txtnama.getText();
String jumlah = txtjumlah.getText();
String harga = txtharga.getText();
String total = txttotal.getText();
try {
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="UPDATE data_penjualan SET KODE_BARANG='"+kode_barang+"', NAMA_BARANG='"+nama_barang+"',JUMLAH='"+jumlah+"',HARGA='"+harga+"',TOTAL='"+total+"' WHERE no_faktur LIKE '"+no_faktur+"'";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null, "Data berhasil diedit..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}                                     

private void bthapusActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
    String no_faktur=txtno_faktur.getText();
try{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"Admin","admin");
Statement statement=con.createStatement();
String sql="DELETE FROM data_penjualan WHERE No_faktur LIKE '"+no_faktur+"'";
statement.executeUpdate(sql);
statement.close();
txtno_faktur.setText("");
txtkode.setText("");
txtnama.setText("");
txtjumlah.setText("");
txtharga.setText("");
txttotal.setText("");
JOptionPane.showMessageDialog(null,"Data berhasil dihapus..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception:"+e.getMessage());
}                                  
}                                      

private void btbatalActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
  txtno_faktur.setText("");
    txtkode.setText("");
    txtnama.setText("");
     txtjumlah.setText("");
     txtharga.setText("");
     txttotal.setText("");
    cmbno.setSelectedIndex(0);
   
}                                      

private void btkeluarActionPerformed(java.awt.event.ActionEvent evt) {                                        
int x;
    x=JOptionPane.showConfirmDialog(null, "Anda Yakin Ingin keluar","Informasi",JOptionPane.YES_NO_OPTION);
    if(x==0)
dispose();// TODO add your handling code here:
}                                      

-Untuk Form Data Konsumen. Source package > Klik kanan > new > JFrame form > Beri nama > finish
Designnya :

Kodingnya :

package deden_herdiansyah;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import java.awt.*;
import java.sql.ResultSet;

/**
 *
 * @author Darryl
 */
public class form_konsumen extends javax.swing.JFrame {
public Connection con;
public Statement statement;
String Laki;
private Dimension UM = Toolkit.getDefaultToolkit().getScreenSize();
   
    /** Creates new form form_konsumen */
    public form_konsumen() {
        initComponents();
    }
private void isicombokode() {
        try{
koneksi();
String sql = "Select * from data_konsumen";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
{
cmbkode.addItem(rs.getString(1)); }
} catch (Exception e){} }
private void cari_kode_konsumen()
{
try{
koneksi();
String sql = "Select * from data_konsumen where kode_konsumen ='"+ cmbkode.getSelectedItem()+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtkode.setText(rs.getString(1)); 
txtnama.setText(rs.getString(2));
txtjenis.setText(rs.getString(3));
txtno.setText(rs.getString(4));
txtalamat.setText(rs.getString(5));
JOptionPane.showMessageDialog(null, "Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Data tidak ditemukan ","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}  


private void koneksi(){
    try {
//String dbname="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:uasTia","Admin","admin");
statement = con.createStatement();
}catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
}
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
// TODO add your handling code here:
    Laki=("Laki-laki");
    txtjenis.setText(String.valueOf(Laki));
}                                            

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                             
// TODO add your handling code here:
    Laki=("Perempuan");
    txtjenis.setText(String.valueOf(Laki));
}                                            

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

private void btcariActionPerformed(java.awt.event.ActionEvent evt) {                                      
// TODO add your handling code here:
   String kode_konsumen = txtkode.getText();
try
{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="SELECT * FROM data_konsumen WHERE kode_konsumen like '"+kode_konsumen+"'";
 ResultSet rs = statement.executeQuery(sql);
if (rs.next())
{
txtnama.setText(rs.getString(2));
txtjenis.setText(rs.getString(3));
txtno.setText(rs.getString(4));
txtalamat.setText(rs.getString(5));
JOptionPane.showMessageDialog(null, "Data ditemukan","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Data tidak ditemukan ","Insert Data",JOptionPane.INFORMATION_MESSAGE);
}
statement.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}    
}                                     

private void cbsimpanActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
     String kode_konsumen = txtkode.getText();
String nama_konsumen = txtnama.getText();
String jenis_kelamin = txtjenis.getText();
String telepon = txtno.getText();
String alamat = txtalamat.getText();
try
{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="insert into data_konsumen values('"+kode_konsumen+"','"+nama_konsumen+"','"+jenis_kelamin+"','"+telepon+"','"+alamat+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null, "Data berhasil dimasukkan..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
txtkode.setText("");
txtnama.setText("");
txtjenis.setText("");
txtno.setText("");
txtalamat.setText("");
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}                                       

private void cbubahActionPerformed(java.awt.event.ActionEvent evt) {                                      
// TODO add your handling code here:
    String kode_konsumen = txtkode.getText();
String nama_konsumen = txtnama.getText();
String jenis_kelamin = txtjenis.getText();
String telepon = txtno.getText();
String alamat = txtalamat.getText();
try {
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbname,"Admin","admin");
Statement statement = con.createStatement();
String sql="UPDATE data_konsumen SET  NAMA_KONSUMEN='"+nama_konsumen+"',JENIS_KELAMIN='"+jenis_kelamin+"',TELEPON='"+telepon+"',ALAMAT='"+alamat+"' WHERE kode_konsumen LIKE '"+kode_konsumen+"'";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null, "Data berhasil diedit..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Eror: "+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception: "+e.getMessage());
}
}                                     

private void cbhapusActionPerformed(java.awt.event.ActionEvent evt) {                                       
// TODO add your handling code here:
     String kode_konsumen=txtkode.getText();
try{
String dbname="jdbc:odbc:uasTia";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"Admin","admin");
Statement statement=con.createStatement();
String sql="DELETE FROM data_konsumen WHERE Kode_konsumen LIKE '"+kode_konsumen+"'";
statement.executeUpdate(sql);
statement.close();
txtkode.setText("");
txtnama.setText("");
txtjenis.setText("");
txtno.setText("");
txtalamat.setText("");
JOptionPane.showMessageDialog(null,"Data berhasil dihapus..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Eror:"+e,"Gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception:"+e.getMessage());
}                             
}                                      

private void cbbatalActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
    txtkode.setText("");
    txtnama.setText("");
    txtalamat.setText("");
     txtno.setText("");
     txtjenis.setText("");
     cmbkode.setSelectedIndex(0);
     buttonGroup1.clearSelection();
  
}                                      

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
int x;
    x=JOptionPane.showConfirmDialog(null, "Anda Yakin Ingin keluar","Informasi",JOptionPane.YES_NO_OPTION);
    if(x==0)
dispose();// TODO add your handling code here:
}                                         
-Setelah sudah.buatlah kembali main class nya
Source package klik kanan > New > java class > beri nama > finish
Ketik kodingnya :

package deden_herdiansyah;

/**
 *
 * @author Darryl
 */
public class main {
  /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {
        // TODO code application logic here
         new formlogin().setVisible(true);
    }
}





0 komentar:

Posting Komentar