koneksi java dengan msAcces
kali ini postingan saya mengenai koneksi aplikasi java dengan ms acces (netbean IDE) hal pertama yang kita lakukan adalah membuat database x dulu...........
pertama buka ms acces ==> lalu buat database baru,beri nama ==> lalu isikan nama-nama kolom : nim = number,nama = text,alamat = text ==> lalu beri nama tabel "mahasiswa" ==> save dalam format 2000-2003 (*.mdb)
step berikutnya setting ODBC
"what is ODBC" ODBC is bla,bla,bla :D
Open Database Connectivity (disingkat menjadi ODBC) adalah sebuah standar terbuka untuk konektivitas antar mesin basis data. Standar ini menyediakan API yang dapat digunakan untuk menjalankan dan mengoneksikan sebuah aplikasi dengan sebuah sistem manajemen basis data (SMBD). Para desainer ODBC membuatnya dengan tujuan agar ODBC terbebas dari penggunaan bahasa pemrograman tertentu, sistem manajemen basis data tertentu, dan sistem operasi tertentu.
caranya :
buka control panel
pilih administrative tool
pilih data source (ODBC)
setelah beres ni contoh source codex :
import javax.swing.JOptionPane;
import java.sql.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* koneksi.java
*
* Created on 10 Apr 12, 16:02:19
*/
/**
*
* @author matt
*/
public class koneksi extends javax.swing.JFrame {
/** Creates new form koneksi */
public koneksi() {
initComponents();
}
/** 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">//GEN-BEGIN:initComponents
private void initComponents() {
nimku = new javax.swing.JLabel();
namaku = new javax.swing.JLabel();
alamatku = new javax.swing.JLabel();
txnim = new javax.swing.JTextField();
txnama = new javax.swing.JTextField();
txalamat = new javax.swing.JTextField();
btntambah = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
nimku.setText("NIM");
namaku.setText("NAMA");
alamatku.setText("ALAMAT");
txnim.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txnimActionPerformed(evt);
}
});
txnama.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txnamaActionPerformed(evt);
}
});
btntambah.setText("TAMBAH");
btntambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btntambahActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
jButton1.setText("TABEL MAHASISWA");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(namaku)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txnama, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(alamatku)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btntambah)
.addComponent(txalamat, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(nimku)
.addGap(39, 39, 39)
.addComponent(txnim, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nimku)
.addComponent(txnim, 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(namaku)
.addComponent(txnama, 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(alamatku)
.addComponent(txalamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addComponent(btntambah)
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntambahActionPerformed
String nim = txnim.getText();
String nama = txnama.getText();
String alamat = txalamat.getText();
try {
String dbname="jdbc:odbc:database1";//nama odbc
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");//user & pasword
Statement statement = con.createStatement();
String sql = "insert into mahasiswa values('"+nim+"','"+nama+"','"+alamat+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"data berhasil di masukan..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"error:"+e,"gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception""+e.getMessage());
}
// TODO add your handling code here:
}//GEN-LAST:event_btntambahActionPerformed
private void txnimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txnimActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txnimActionPerformed
private void txnamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txnamaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txnamaActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JOptionPane.showMessageDialog(null,"Cr34t3D by M4tt & fishb0n3"); // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new koneksi().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel alamatku;
private javax.swing.JButton btntambah;
private javax.swing.JButton jButton1;
private javax.swing.JLabel namaku;
private javax.swing.JLabel nimku;
private javax.swing.JTextField txalamat;
private javax.swing.JTextField txnama;
private javax.swing.JTextField txnim;
// End of variables declaration//GEN-END:variables
}
next time i will explain how to update
pertama buka ms acces ==> lalu buat database baru,beri nama ==> lalu isikan nama-nama kolom : nim = number,nama = text,alamat = text ==> lalu beri nama tabel "mahasiswa" ==> save dalam format 2000-2003 (*.mdb)
step berikutnya setting ODBC
"what is ODBC" ODBC is bla,bla,bla :D
Open Database Connectivity (disingkat menjadi ODBC) adalah sebuah standar terbuka untuk konektivitas antar mesin basis data. Standar ini menyediakan API yang dapat digunakan untuk menjalankan dan mengoneksikan sebuah aplikasi dengan sebuah sistem manajemen basis data (SMBD). Para desainer ODBC membuatnya dengan tujuan agar ODBC terbebas dari penggunaan bahasa pemrograman tertentu, sistem manajemen basis data tertentu, dan sistem operasi tertentu.
caranya :
buka control panel
pilih administrative tool
pilih data source (ODBC)
setelah beres ni contoh source codex :
import javax.swing.JOptionPane;
import java.sql.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* koneksi.java
*
* Created on 10 Apr 12, 16:02:19
*/
/**
*
* @author matt
*/
public class koneksi extends javax.swing.JFrame {
/** Creates new form koneksi */
public koneksi() {
initComponents();
}
/** 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">//GEN-BEGIN:initComponents
private void initComponents() {
nimku = new javax.swing.JLabel();
namaku = new javax.swing.JLabel();
alamatku = new javax.swing.JLabel();
txnim = new javax.swing.JTextField();
txnama = new javax.swing.JTextField();
txalamat = new javax.swing.JTextField();
btntambah = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
nimku.setText("NIM");
namaku.setText("NAMA");
alamatku.setText("ALAMAT");
txnim.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txnimActionPerformed(evt);
}
});
txnama.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txnamaActionPerformed(evt);
}
});
btntambah.setText("TAMBAH");
btntambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btntambahActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
jButton1.setText("TABEL MAHASISWA");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(namaku)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txnama, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(alamatku)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btntambah)
.addComponent(txalamat, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(nimku)
.addGap(39, 39, 39)
.addComponent(txnim, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nimku)
.addComponent(txnim, 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(namaku)
.addComponent(txnama, 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(alamatku)
.addComponent(txalamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(38, 38, 38)
.addComponent(btntambah)
.addContainerGap(46, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btntambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntambahActionPerformed
String nim = txnim.getText();
String nama = txnama.getText();
String alamat = txalamat.getText();
try {
String dbname="jdbc:odbc:database1";//nama odbc
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(dbname,"admin","admin");//user & pasword
Statement statement = con.createStatement();
String sql = "insert into mahasiswa values('"+nim+"','"+nama+"','"+alamat+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null,"data berhasil di masukan..","Insert Data",JOptionPane.INFORMATION_MESSAGE);
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"error:"+e,"gagal",JOptionPane.WARNING_MESSAGE);
//System.err.println("Exception""+e.getMessage());
}
// TODO add your handling code here:
}//GEN-LAST:event_btntambahActionPerformed
private void txnimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txnimActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txnimActionPerformed
private void txnamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txnamaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txnamaActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JOptionPane.showMessageDialog(null,"Cr34t3D by M4tt & fishb0n3"); // TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new koneksi().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel alamatku;
private javax.swing.JButton btntambah;
private javax.swing.JButton jButton1;
private javax.swing.JLabel namaku;
private javax.swing.JLabel nimku;
private javax.swing.JTextField txalamat;
private javax.swing.JTextField txnama;
private javax.swing.JTextField txnim;
// End of variables declaration//GEN-END:variables
}
next time i will explain how to update
Komentar
Posting Komentar