Salamilmu's Blog

Home » Pemrograman » Koneksi Netbeans dengan MS Access

Koneksi Netbeans dengan MS Access

July 2014
M T W T F S S
« Nov    
 123456
78910111213
14151617181920
21222324252627
28293031  

Selama ini saya hanya membuat dan mengulas tentang aplikasi database menggunakan VB.Net dengan MS Access, kali saya coba membuat sebuah aplikasi menggunakan Netbeans dengan MS Access.

Baik langsung saja buka MS Access anda, pertama sekali kita buat terlebih dahulu sebuah database dengan nama POS.mdb, simpan kemudian buat sebuah table dengan nama Barang kemudian desain field-fieldnya seperti dibawah ini.

pos1

Jika sudah selesai membuat dan mendesain database, sekarang anda buka Netbeans anda. Pada box Category pilih Java, pada box Projects pilih Java Desktop Aplication lalu pilih Next.

pos2

Pada Project Name ganti menjadi PointOfSale, sedangkan pada Project Location, Project Folder dan Application Class biarkan seperti itu. Pada box Choose Application Shell pilih Basic Application.

pos3

Kemudian pilih Finish dan hasilnya akan tampak seperti dibawah ini.

pos4

Tambahkan sebuah menulist untuk memanggil form Barang nantinya dengan cara klik kanan pada menubar yang terdapat pada Tab Inspector sebelah kiri jendela Netbeans anda kemudian pilih Add Menu.

pos6

Pada menulist yang telah kita buat tadi, klik kanan kemudian Edit Text ganti menjadi Data.

Tambahkan lagi sebuah menulist dengan cara klik kanan pada menu item yang kita buat tadi lalu pilih Add From Pallet => Menu Item.

Pada sub menu item yang baru kita buat tadi, ganti keterangannya dengan cara klik kanan pilih Edit Text ganti menjadi Barang. Hasilnya seperti gambar dibawah ini.

pos9

Selanjutnya kita tambahkan sebuat object form, pada tab Projects klik kanan di sub root tree pointofsale pilih New => JFrame Form. Pada Class name ganti menjadi frmBarang lalu pilih Finish.

pos11

Sekarang kita desain frmBarang dengan menambahkan beberapa komponen seperti JTextField, JLabel, JButton dan JTable, kemudian pada objek JTextField ganti setiap namanya dengan cara klik kanan pada objek JTextField => Change Varible Name dan ganti name sesuai dengan fungsinya. Berikut hasil desainnya.

pos14

Sekarang mulai membuat kodingnya, langkah selanjutnya kita buat sebuah sintak untuk memanggil frmBarang dengan cara kembali ke form utama klik kanan pada sub menu item pilih Events => Action => actionPerformed.

Lalu ketikkan sintak berikut ini

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

frmBarang frm = new frmBarang();

frm.setVisible(true);

}

Kemudian kita buat sebuah Class yang berfungsi untuk menyimpan koneksi ke database. Klik kanan di sub root tree pointofsale pilih New => Java Class

pos16

Pada Class Name ganti menjadi clsKoneksi kemudian Finish.

Kemudian ketikkan sintak atau koding dibawah ini.

package pointofsale;

 

import java.sql.*;

import javax.swing.JOptionPane;

 

public class clsKoneksi {

Connection Conn = null;

ResultSet rs = null;

PreparedStatement pst = null;

 

public static Connection Koneksi(){

try{

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Connection Conn = DriverManager.getConnection(“jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=POS.mdb”);

return Conn;

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

return null;

}

}

}

Ok string koneksi sudah selesai, sekarang kita mulai membuat perintah SQL seperti Insert, Update, Select dan Delete.

Kembali lagi ke frmBarang klik kanan pada objek JButton Simpan pilih Events => Action => actionPerformed

Kemudian ketikkan sintak dibawah ini.

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String sql = “insert into barang values(‘” + txtKodeBrg.getText() + “‘,'” + txtNamaBrg.getText()

+”‘,'” + txtHrgBeli.getText() + “‘,'” + txtHrgJual.getText() + “‘,'” + txtQty.getText() + “‘)”;

try{

pst = Conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null,”Data Sudah Disimpan!”);

Tampil_Data();

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

Berikut listing code selengkapnya pada frmBarang

package pointofsale;

 

/**

*

* @author AHMAD

*/

import java.sql.*;

import javax.swing.JOptionPane;

import net.proteanit.sql.DbUtils;

 

public class frmBarang extends javax.swing.JFrame {

 

/** Creates new form frmBarang */

public frmBarang() {

initComponents();

}

 

Connection Conn = null;

ResultSet rs = null;

PreparedStatement pst = null;

/** 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”)

// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>

private void initComponents() {

 

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

jLabel5 = new javax.swing.JLabel();

txtKodeBrg = new javax.swing.JTextField();

txtNamaBrg = new javax.swing.JTextField();

txtHrgBeli = new javax.swing.JTextField();

txtHrgJual = new javax.swing.JTextField();

txtQty = new javax.swing.JTextField();

jScrollPane1 = new javax.swing.JScrollPane();

tblBarang = new javax.swing.JTable();

btnSimpan = new javax.swing.JButton();

btnUbah = new javax.swing.JButton();

Hapus = new javax.swing.JButton();

btnCari = new javax.swing.JButton();

Keluar = new javax.swing.JButton();

 

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setName(“Form”); // NOI18N

addWindowListener(new java.awt.event.WindowAdapter() {

public void windowOpened(java.awt.event.WindowEvent evt) {

formWindowOpened(evt);

}

});

 

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pointofsale.PointOfSaleApp.class).getContext().getResourceMap(frmBarang.class);

jLabel1.setText(resourceMap.getString(“jLabel1.text”)); // NOI18N

jLabel1.setName(“jLabel1”); // NOI18N

 

jLabel2.setText(resourceMap.getString(“jLabel2.text”)); // NOI18N

jLabel2.setName(“jLabel2”); // NOI18N

 

jLabel3.setText(resourceMap.getString(“jLabel3.text”)); // NOI18N

jLabel3.setName(“jLabel3”); // NOI18N

 

jLabel4.setText(resourceMap.getString(“jLabel4.text”)); // NOI18N

jLabel4.setName(“jLabel4”); // NOI18N

 

jLabel5.setText(resourceMap.getString(“jLabel5.text”)); // NOI18N

jLabel5.setName(“jLabel5”); // NOI18N

 

txtKodeBrg.setText(resourceMap.getString(“txtKodeBrg.text”)); // NOI18N

txtKodeBrg.setName(“txtKodeBrg”); // NOI18N

 

txtNamaBrg.setName(“txtNamaBrg”); // NOI18N

 

txtHrgBeli.setText(resourceMap.getString(“txtHrgBeli.text”)); // NOI18N

txtHrgBeli.setName(“txtHrgBeli”); // NOI18N

 

txtHrgJual.setText(resourceMap.getString(“txtHrgJual.text”)); // NOI18N

txtHrgJual.setName(“txtHrgJual”); // NOI18N

 

txtQty.setText(resourceMap.getString(“txtQty.text”)); // NOI18N

txtQty.setName(“txtQty”); // NOI18N

 

jScrollPane1.setName(“jScrollPane1”); // NOI18N

 

tblBarang.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 [] {

“Title 1”, “Title 2”, “Title 3”, “Title 4”

}

));

tblBarang.setName(“tblBarang”); // NOI18N

jScrollPane1.setViewportView(tblBarang);

 

btnSimpan.setText(resourceMap.getString(“btnSimpan.text”)); // NOI18N

btnSimpan.setName(“btnSimpan”); // NOI18N

btnSimpan.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnSimpanActionPerformed(evt);

}

});

 

btnUbah.setText(resourceMap.getString(“btnUbah.text”)); // NOI18N

btnUbah.setName(“btnUbah”); // NOI18N

btnUbah.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnUbahActionPerformed(evt);

}

});

 

Hapus.setText(resourceMap.getString(“Hapus.text”)); // NOI18N

Hapus.setName(“Hapus”); // NOI18N

Hapus.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

HapusActionPerformed(evt);

}

});

 

btnCari.setText(resourceMap.getString(“btnCari.text”)); // NOI18N

btnCari.setName(“btnCari”); // NOI18N

btnCari.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

btnCariActionPerformed(evt);

}

});

 

Keluar.setText(resourceMap.getString(“Keluar.text”)); // NOI18N

Keluar.setName(“Keluar”); // NOI18N

Keluar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

KeluarActionPerformed(evt);

}

});

 

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(48, 48, 48)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel5)

.addComponent(jLabel4)

.addComponent(jLabel3)

.addComponent(jLabel2)

.addComponent(jLabel1))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(txtQty, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)

.addComponent(txtHrgJual, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)

.addComponent(txtNamaBrg, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)

.addComponent(txtHrgBeli, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)

.addComponent(txtKodeBrg, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))

.addGap(88, 88, 88))))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addContainerGap(185, Short.MAX_VALUE)

.addComponent(btnSimpan)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnUbah)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(Hapus)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(btnCari)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(Keluar)

.addGap(159, 159, 159))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(42, 42, 42)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(txtKodeBrg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(txtNamaBrg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3)

.addComponent(txtHrgBeli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(txtHrgJual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel5)

.addComponent(txtQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(btnSimpan)

.addComponent(btnUbah)

.addComponent(Hapus)

.addComponent(btnCari)

.addComponent(Keluar))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(19, 19, 19))

);

 

pack();

}// </editor-fold>

 

private void formWindowOpened(java.awt.event.WindowEvent evt) {

// TODO add your handling code here:

Conn = clsKoneksi.Koneksi();

super.setLocationRelativeTo(null);

Tampil_Data();

}

 

private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String sql = “insert into barang values(‘” + txtKodeBrg.getText() + “‘,'” + txtNamaBrg.getText()

+”‘,'” + txtHrgBeli.getText() + “‘,'” + txtHrgJual.getText() + “‘,'” + txtQty.getText() + “‘)”;

try{

pst = Conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null,”Data Sudah Disimpan!”);

Tampil_Data();

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

 

private void KeluarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

dispose();

}

 

private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String sql = “update barang set NamaBrg= ‘” + txtNamaBrg.getText()

+”‘,HrgBeli='” + txtHrgBeli.getText() + “‘,” +

“HrgJual='” + txtHrgJual.getText() + “‘,” +

“Qty='” + txtQty.getText() + “‘” +

” where KODEBRG='” + txtKodeBrg.getText() + “‘”;

try{

pst = Conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null,”Data Sudah Disimpan!”);

Tampil_Data();

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

 

private void btnCariActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String cari =”select * from barang where kodebrg='” + txtKodeBrg.getText() + “‘”;

try{

pst = Conn.prepareStatement(cari);

rs = pst.executeQuery();

if (rs.next()){

txtKodeBrg.setText(rs.getString(“kodebrg”));

txtNamaBrg.setText(rs.getString(“namabrg”));

txtHrgBeli.setText(rs.getString(“Hrgbeli”));

txtHrgJual.setText(rs.getString(“hrgjual”));

txtQty.setText(rs.getString(“qty”));

}else{

JOptionPane.showMessageDialog(null, “Data yang dicari tidak ada”);

}

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

 

private void HapusActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String sql = “delete from barang where kodebrg='” + txtKodeBrg.getText() + “‘”;

try{

int replay = JOptionPane.showConfirmDialog(this,

“Yakin akan menghapus data dengan Kode Barang = ‘” + txtKodeBrg.getText() + “‘”,

“KOnfirmasi”,JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

if (replay== JOptionPane.YES_OPTION){

pst = Conn.prepareStatement(sql);

pst.execute();

JOptionPane.showMessageDialog(null,”Data Dengan Kode Barang = ‘” + txtKodeBrg.getText() + “‘ Berhasil Dihapus!”,”Perhatian”,JOptionPane.INFORMATION_MESSAGE);

Tampil_Data();

Bersih();

}

 

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

 

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new frmBarang().setVisible(true);

}

});

}

 

private void Tampil_Data(){

String sql = “select * from barang order by kodebrg asc”;

try{

pst = Conn.prepareStatement(sql);

rs = pst.executeQuery();

tblBarang.setModel(DbUtils.resultSetToTableModel(rs));

}catch(Exception e){

JOptionPane.showMessageDialog(null, e);

}

}

 

private void Bersih(){

txtKodeBrg.setText(“”);

txtNamaBrg.setText(“”);

txtHrgBeli.setText(“”);

txtHrgJual.setText(“”);

txtQty.setText(“”);

txtKodeBrg.requestFocus();

}

// Variables declaration – do not modify

private javax.swing.JButton Hapus;

private javax.swing.JButton Keluar;

private javax.swing.JButton btnCari;

private javax.swing.JButton btnSimpan;

private javax.swing.JButton btnUbah;

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.JScrollPane jScrollPane1;

private javax.swing.JTable tblBarang;

private javax.swing.JTextField txtHrgBeli;

private javax.swing.JTextField txtHrgJual;

private javax.swing.JTextField txtKodeBrg;

private javax.swing.JTextField txtNamaBrg;

private javax.swing.JTextField txtQty;

// End of variables declaration

 

}

Dan berikut hasil dari program yang saya buat tadi.

pos19

Untuk lebih jelas, silahkan download contoh programnya disini http://www.4shared.com/rar/3eb8x6SCba/PointOfSale.html

Semoga bermanfaat.

 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: