Senin, 21 Januari 2013
0 komentar

Program Sederhana Pengiriman Surat Kantor Pos



Berikut ini adalah contoh program aplikasi sederhana mengenai pengiriman surat di kantor pos
 1. Buat tampilan program,misalnya seperti ini



2. Source Code dari form di atas adalah sebagai berikut :

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

/*
 * FormSurat.java
 *
 * Created on Jan 19, 2013, 5:25:57 PM
 */
package surat;

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 FormSurat extends javax.swing.JFrame {

    Connection konek;
    ResultSet rs;
    Statement stmt;
    private Object[][] dataTabel = null;
    private String[] header = {"NAMA PENGIRIM", "ALAMAT PENGIRIM", "TUJUAN", "BIAYA"};
    private String[] headerBiaya = {"KOTA TUJUAN", "BIAYA"};

    public FormSurat() {
        initComponents();
        bukakoneksi();
        tabelPengiriman();
        tabelBiaya();
        tujuan();
    }

    public void hapus() {
        jTextFieldKota.setText(null);
        jTextFieldTarif.setText(null);
        jTextFieldAlmt.setText(null);
        jTextFieldKota.setText(null);
        jComboBoxTujuan.setSelectedItem(null);
    }

    private void bukakoneksi() {
        try {
            database dataDB = new database("localhost", "root", "", "surat");
            konek = dataDB.konekdatabase();
        } catch (Exception e) {
            System.out.println("Error : " + e);
        }
    }

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

            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("nama_pengirim");
                dataTabel[x][1] = rs.getString("alamat");
                dataTabel[x][2] = rs.getString("tujuan");
                dataTabel[x][3] = rs.getString("tarif");
                x++;
            }
            jTablePengirim.setModel(new DefaultTableModel(dataTabel, header));
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

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

            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("kota_tujuan");
                dataTabel[x][1] = rs.getString("tarif");
                x++;
            }
            jTableBiaya.setModel(new DefaultTableModel(dataTabel, headerBiaya));
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    private void tujuan() {
        try {
            stmt = (Statement) konek.createStatement();
            rs = stmt.executeQuery("select kota_tujuan from biaya");
            ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
            int count = 0;
            while (rs.next()) {
                jComboBoxTujuan.insertItemAt(rs.getString("kota_tujuan"), 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() {

        jPanelBiaya = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldKota = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldTarif = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableBiaya = new javax.swing.JTable();
        jButtonSimpan = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanelSurat = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldNama = new javax.swing.JTextField();
        jTextFieldAlmt = new javax.swing.JTextField();
        jComboBoxTujuan = new javax.swing.JComboBox();
        jButtonProses = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTablePengirim = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelBiaya.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Biaya Pengiriman"));

        jLabel1.setText("KOTA TUJUAN");

        jLabel3.setText("TARIF");

        jTableBiaya.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTableBiaya.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 [] {
                "KOTA TUJUAN", "BIAYA PENGIRIMAN"
            }
        ));
        jScrollPane1.setViewportView(jTableBiaya);

        jButtonSimpan.setText("Save");
        jButtonSimpan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSimpanMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelBiayaLayout = new javax.swing.GroupLayout(jPanelBiaya);
        jPanelBiaya.setLayout(jPanelBiayaLayout);
        jPanelBiayaLayout.setHorizontalGroup(
            jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBiayaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextFieldTarif)
                        .addComponent(jTextFieldKota, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
                    .addComponent(jButtonSimpan))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
        );
        jPanelBiayaLayout.setVerticalGroup(
            jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBiayaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                    .addGroup(jPanelBiayaLayout.createSequentialGroup()
                        .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextFieldKota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelBiayaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldTarif, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSimpan)
                        .addContainerGap(76, Short.MAX_VALUE))))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("BIAYA PENGIRIMAN SURAT KANTOR POS SIDORAJO");

        jPanelSurat.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Pengiriman Surat"));

        jLabel4.setText("NAMA PENGIRIM");

        jLabel5.setText("ALAMAT PENGIRIM");

        jLabel6.setText("TUJUAN");

        jButtonProses.setText("Proses");
        jButtonProses.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonProsesMouseClicked(evt);
            }
        });

        jTablePengirim.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},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "NAMA PENGIRIM", "ALAMAT PENGIRIM", "TUJUAN", "BIAYA"
            }
        ));
        jScrollPane2.setViewportView(jTablePengirim);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/surat/gambar/Pos_Indonesia_logo..png"))); // NOI18N

        javax.swing.GroupLayout jPanelSuratLayout = new javax.swing.GroupLayout(jPanelSurat);
        jPanelSurat.setLayout(jPanelSuratLayout);
        jPanelSuratLayout.setHorizontalGroup(
            jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSuratLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelSuratLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(30, 30, 30)
                        .addComponent(jTextFieldNama, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelSuratLayout.createSequentialGroup()
                        .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxTujuan, 0, 156, Short.MAX_VALUE)
                            .addComponent(jTextFieldAlmt))))
                .addGap(18, 18, 18)
                .addComponent(jButtonProses)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
        );
        jPanelSuratLayout.setVerticalGroup(
            jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSuratLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSuratLayout.createSequentialGroup()
                        .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextFieldNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonProses))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextFieldAlmt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelSuratLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jComboBoxTujuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelBiaya, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
            .addComponent(jPanelSurat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(15, 15, 15)
                .addComponent(jPanelBiaya, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelSurat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }//                         

    private void jButtonSimpanMouseClicked(java.awt.event.MouseEvent evt) {                                           
        String kota = jTextFieldKota.getText();
        String trf = jTextFieldTarif.getText();
        String id = "0";
        if ("".equals(kota) || "".equals(trf)) {
            JOptionPane.showMessageDialog(jPanelBiaya, "Data tidak boleh kosong", "Warning", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                stmt.executeUpdate("INSERT INTO biaya VALUES ('" + id + "','" + kota + "','" + trf + "')");
                tabelBiaya();
                hapus();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    }                                          

    private void jButtonProsesMouseClicked(java.awt.event.MouseEvent evt) {                                           
        String nm = jTextFieldNama.getText();
        String almt = jTextFieldAlmt.getText();
        String tuju = (String) jComboBoxTujuan.getSelectedItem();
        String id = "0";
        if ("".equals(nm) || "".equals(almt) || tuju == null) {
            JOptionPane.showMessageDialog(jPanelSurat, "Data tidak boleh kosong", "Warning", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                stmt.executeUpdate("INSERT INTO pengiriman VALUES ('" + id + "','" + nm + "','" + almt + "','" + tuju + "',(select tarif from biaya where kota_tujuan='" + tuju + "'))");
                tabelPengiriman();
                hapus();
            } 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(FormSurat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormSurat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormSurat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormSurat.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 FormSurat().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonProses;
    private javax.swing.JButton jButtonSimpan;
    private javax.swing.JComboBox jComboBoxTujuan;
    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.JPanel jPanelBiaya;
    private javax.swing.JPanel jPanelSurat;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableBiaya;
    private javax.swing.JTable jTablePengirim;
    private javax.swing.JTextField jTextFieldAlmt;
    private javax.swing.JTextField jTextFieldKota;
    private javax.swing.JTextField jTextFieldNama;
    private javax.swing.JTextField jTextFieldTarif;
    // End of variables declaration                   
}
3. SourceCode Koneksi dengan database mysql
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package surat;

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

/**
 *
 * @author Otz
 */
public class database {

    private final String url;
    private final String usr;
    private final String pwd;

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

    public Connection konekdatabase() throws SQLException {
        Connection konek = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            konek = (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 konek;
    }
}
4. Jalankan program maka hasilnya akan seperti ini :

5. Untuk koneksi mysql silahkan download mysql konektor disini
6. Untuk database silahkan donwload disini

7. Download full program
 
Toggle Footer
Top