Senin, 21 Januari 2013
3 komentar

Program menabung sederhana dengan java Netbeans



Program ini adalah program untuk menabung secara sederhana dengan mengunakan netbeans
1. Buat tampilan program seperti dibawah ini :
2. Source Code untuk membuat tampilan di atas adalah sebagai berikut :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormUtama.java
 *
 * Created on Jan 19, 2013, 1:27:19 PM
 */
package nabung;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Otz
 */
public class FormUtama extends javax.swing.JFrame {

    Connection konek;
    ResultSet rs;
    Statement stmt;
    private Object[][] dataTabel = null;
    private String[] header = {"NOMER REKENING", "NAMA NASABAH", "ALAMAT NASABAH", "SALDO"};

    /** Creates new form FormUtama */
    public FormUtama() {
        initComponents();
        koneksidb();
        tabelNasabah();
        rekening();
    }

    public void clear() {
        jTextFieldKode.setText(null);
        jTextFieldNama.setText(null);
        jTextFieldamlt.setText(null);
        jTextFieldSaldoAwal.setText(null);
        jTextFieldNom.setText(null);
        jTextFieldNom2.setText(null);
        jComboBoxRek.setSelectedItem(null);
        jComboBoxRekTun.setSelectedItem(null);
    }

    private void koneksidb() {
        try {
            ClassKoneksi kon = new ClassKoneksi("localhost", "root", "lancarsilver", "bank");
            konek = kon.getConnection();
        } catch (Exception e) {
            System.out.println("Error : " + e);
        }
    }

    private void tabelNasabah() {
        try {
            stmt = (Statement) konek.createStatement();
            rs = stmt.executeQuery("select * from nasabah");

            ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
            int col = meta.getColumnCount();
            int baris = 0;
            while (rs.next()) {
                baris = rs.getRow();
            }
            dataTabel = new Object[baris][col];
            int x = 0;
            rs.beforeFirst();
            while (rs.next()) {
                dataTabel[x][0] = rs.getString("rek");
                dataTabel[x][1] = rs.getString("nama");
                dataTabel[x][2] = rs.getString("alamat");
                dataTabel[x][3] = rs.getString("saldo");
                x++;
            }
            jTableNasabah.setModel(new DefaultTableModel(dataTabel, header));
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    private void rekening() {
        try {
            stmt = (Statement) konek.createStatement();
            rs = stmt.executeQuery("select rek from nasabah");
            ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
            int count = 0;
            while (rs.next()) {
                jComboBoxRek.insertItemAt(rs.getString("rek"), count);
                jComboBoxRekTun.insertItemAt(rs.getString("rek"), count);
                count++;
            }
        } 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")
    //                           
    private void initComponents() {

        jPanelNasabah = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldKode = new javax.swing.JTextField();
        jTextFieldNama = new javax.swing.JTextField();
        jTextFieldamlt = new javax.swing.JTextField();
        jButtonInput = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldSaldoAwal = new javax.swing.JTextField();
        jPanelTab = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jComboBoxRek = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldNom = new javax.swing.JTextField();
        jButtonNabung = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableNasabah = new javax.swing.JTable();
        jPanelTun = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jComboBoxRekTun = new javax.swing.JComboBox();
        jTextFieldNom2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jButtonProsesAmbil = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FORM TABUNGAN");

        jPanelNasabah.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "INPUT DATA NASABAH"));

        jLabel1.setText("NOMER REKENING");

        jLabel2.setText("NAMA");

        jLabel3.setText("ALAMAT");

        jButtonInput.setText("INPUT");
        jButtonInput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonInputMouseClicked(evt);
            }
        });

        jLabel4.setText("SALDO AWAL");

        javax.swing.GroupLayout jPanelNasabahLayout = new javax.swing.GroupLayout(jPanelNasabah);
        jPanelNasabah.setLayout(jPanelNasabahLayout);
        jPanelNasabahLayout.setHorizontalGroup(
            jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNasabahLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(27, 27, 27)
                .addGroup(jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonInput)
                    .addGroup(jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextFieldKode)
                        .addComponent(jTextFieldamlt, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                        .addComponent(jTextFieldSaldoAwal)
                        .addComponent(jTextFieldNama)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanelNasabahLayout.setVerticalGroup(
            jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNasabahLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelNasabahLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addGap(2, 2, 2))
                    .addGroup(jPanelNasabahLayout.createSequentialGroup()
                        .addComponent(jTextFieldKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldamlt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(9, 9, 9)
                .addGroup(jPanelNasabahLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldSaldoAwal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButtonInput)
                .addContainerGap())
        );

        jPanelTab.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "INPUT TABUNGAN"));

        jLabel5.setText("NOMER REKENING");

        jLabel6.setText("NOMINAL");

        jButtonNabung.setText("PROSES");
        jButtonNabung.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonNabungMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelTabLayout = new javax.swing.GroupLayout(jPanelTab);
        jPanelTab.setLayout(jPanelTabLayout);
        jPanelTabLayout.setHorizontalGroup(
            jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonNabung)
                    .addGroup(jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextFieldNom)
                        .addComponent(jComboBoxRek, 0, 144, Short.MAX_VALUE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanelTabLayout.setVerticalGroup(
            jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxRek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButtonNabung)
                .addContainerGap(59, Short.MAX_VALUE))
        );

        jTableNasabah.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jTableNasabah.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 [] {
                "NOMER REKENING", "NAMA NASABAH", "ALAMAT NASABAH", "SALDO"
            }
        ));
        jScrollPane1.setViewportView(jTableNasabah);

        jPanelTun.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "AMBIL TUNAI"));

        jLabel7.setText("NOMER REKENING");

        jLabel8.setText("NOMINAL");

        jButtonProsesAmbil.setText("PROSES");
        jButtonProsesAmbil.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonProsesAmbilMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelTunLayout = new javax.swing.GroupLayout(jPanelTun);
        jPanelTun.setLayout(jPanelTunLayout);
        jPanelTunLayout.setHorizontalGroup(
            jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTunLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonProsesAmbil)
                    .addComponent(jTextFieldNom2, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(jComboBoxRekTun, 0, 139, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelTunLayout.setVerticalGroup(
            jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTunLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jComboBoxRekTun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelTunLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNom2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonProsesAmbil)
                .addContainerGap(66, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelNasabah, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelTab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelTun, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 923, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTun, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelNasabah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }//                         

    private void jButtonInputMouseClicked(java.awt.event.MouseEvent evt) {                                          
        String kd = jTextFieldKode.getText();
        String nm = jTextFieldNama.getText();
        String alamat = jTextFieldamlt.getText();
        String saldoAwal = jTextFieldSaldoAwal.getText();
        if (kd.equals("") || nm.equals("") || alamat.equals("") || saldoAwal.equals("")) {
            JOptionPane.showMessageDialog(jPanelNasabah, "Data tidak boleh kosong", "Warning", JOptionPane.ERROR_MESSAGE);

        } else {
            try {
                stmt.executeUpdate("INSERT INTO nasabah VALUES ('" + kd + "','" + nm + "','" + alamat + "','" + saldoAwal + "') ");
                clear();
                tabelNasabah();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    }                                         

    private void jButtonNabungMouseClicked(java.awt.event.MouseEvent evt) {                                           
        String nom = jTextFieldNom.getText();
        String rek = (String) jComboBoxRek.getSelectedItem();
        if ("".equals(nom) || "".equals(rek)) {
            JOptionPane.showMessageDialog(jPanelTab, "Data tidak boleh kosong", "Warning", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                stmt.executeUpdate("update nasabah set saldo=saldo+'" + nom + "'where rek='" + rek + "'");
                clear();
                tabelNasabah();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    }                                          

    private void jButtonProsesAmbilMouseClicked(java.awt.event.MouseEvent evt) {                                                
        String nom2 = jTextFieldNom2.getText();
        String rek2 = (String) jComboBoxRekTun.getSelectedItem();
        if ("".equals(nom2) || "".equals(rek2)) {
            JOptionPane.showMessageDialog(jPanelTun, "Data tidak boleh kosong", "Warning", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                stmt.executeUpdate("update nasabah set saldo=saldo-'" + nom2+ "'where rek='" + rek2 + "'");
                clear();
                tabelNasabah();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    }                                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* 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(FormUtama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormUtama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormUtama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormUtama.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormUtama().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonInput;
    private javax.swing.JButton jButtonNabung;
    private javax.swing.JButton jButtonProsesAmbil;
    private javax.swing.JComboBox jComboBoxRek;
    private javax.swing.JComboBox jComboBoxRekTun;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanelNasabah;
    private javax.swing.JPanel jPanelTab;
    private javax.swing.JPanel jPanelTun;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableNasabah;
    private javax.swing.JTextField jTextFieldKode;
    private javax.swing.JTextField jTextFieldNama;
    private javax.swing.JTextField jTextFieldNom;
    private javax.swing.JTextField jTextFieldNom2;
    private javax.swing.JTextField jTextFieldSaldoAwal;
    private javax.swing.JTextField jTextFieldamlt;
    // End of variables declaration                   
}
3. Membuat ClassKoneksi sebagai berikut :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nabung;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



/**
 *
 * @author Otz
 */
public class ClassKoneksi {
    private final String url;
    private final String usr;
    private final String pwd;

     public ClassKoneksi(String host, String user, String pass, String dbn){
        this.url="jdbc:mysql://"+host+"/"+dbn;
        this.usr=user;
        this.pwd=pass;
    }

    public Connection getConnection() throws SQLException{
        Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=(Connection) DriverManager.getConnection(this.url,this.usr,this.pwd);
        } catch (ClassNotFoundException e) {
            System.out.println("Error #1 : "+e.getMessage());
        } catch (SQLException e) {
            System.out.println("Error #2 : "+e.getMessage());
        }
        return con;
    }
    
   
}
4. import library mysql connector,bisa di download disini
5. Jalankan program, maka akan tampil sebagai berikut :
6. Download full program disini.
 
Toggle Footer
Top