Membuat Database
di Access
1.
Buat database dengan nama DatabaseMahasiswa2 di
Microsoft Access dengan format *.mdb (2002-2003)
2.
Buat tabel berikut:
Field Name
|
Data Type
|
Field Size
|
no
|
Text
|
3
|
nama
|
Text
|
30
|
3.
Simpan dengan nama TableMahasiswa
Membuat
Datasource
1.
Start – Control Panel – Administrative Tools
2.
Klik dua kali Data Sources (ODBC)
3.
Add
4.
Klik Driver do Microsoft Access (*.mdb)
5.
Finish
6.
Ketik: DsMahasiswa2
7.
Select
8.
Cari database yang sudah dibuat di Access,
contohnya DatabaseMahasiswa2
9.
Advanced
10.
Ketik login name: komar; password: komar
(sesuai keinginan)
11.
OK – OK – OK
Script
Koneksi di Netbeans
1.
Masuk ke NetBeans IDE 6.8
2.
File – New Project
3.
Categories: Java; Projects: Java Application
4.
Next
5.
Project Name: JavaApplicationMhsTambah;
Project Location: D:\javalat\posting2\JavaAccessTambah
6.
Hilangkan centang di Create Main Class
7.
Finish
8.
Di panel Projects
9.
Klik tanda + (expand) di JavaApplicationMhsTambah
10.
Klik tanda + di Source Packages
11.
Klik kanan di <default package>
12.
New – Java Package
13.
Package Name: PackageMhsTambah
14.
Finish
15.
Klik kanan PackageMhsTambah di panel Projects
16.
New – Java Class
17.
Class Name: ClassMhsTambah
18.
Finish
19.
Klik dua kali ClassMahasiswa di panel Projects
20.
Ketik script berikut:
/*
* To
change this template, choose Tools | Templates
* and
open the template in the editor.
*/
package PackageMhsTambah;
/**
*
*
@author komtas
*/
import java.sql.*;
import javax.swing.*;
public class ClassMhsTambah {
void testClassMhsTambah()
{
try
{
String
dbname="jdbc:odbc:DsMahasiswa2"; // nama ODBC
// ingat-ingat, CobaDatabase adalah nama ODBC
kita yang udah kita atur sebelumnya
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection(dbname,"komar","komar");
// user n passwordnya
// user database samakan dengan yang kita atur
di ODBC tadi, yaitu
// 'Admin', dengan password 'admin'
JOptionPane.showMessageDialog(null,
"Koneksi untuk tambah data berhasil","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[])
{
ClassMhsTambah test = new ClassMhsTambah();
test.testClassMhsTambah();
System.exit(0);
}
}
21.
Simpan
22.
Run – Run File (untuk menjalankan program)
23.
Hasilnya akan muncul pesan: “Koneksi untuk
tambah data berhasil”
Desain
Form Tambah Data
1.
Klik kanan PackageMhsTambah – New JFrame Form
2.
Class Name: JFrameMhsTambah
3.
Finish
4.
Buat desain sebagai berikut:
5.
Gambar di atas ubah dengan ketentuan:
-
jLabel1®Text: No
-
jLabel2® Text: Nama
-
jTextField1® Text: (Kosongkan); Klik kanan –
Change Variable Name: txtNo
-
jTextField2® Text: (Kosongkan); Klik kanan –
Change Variable Name: txtNama
-
jButton1® Text: Tambah; Klik kanan – Change
Variable Name: btnTambah
Hasilnya:
Script Tombol
Tambah
1.
Klik kanan tombol Tambah
2.
Klik Events – Action – actionPerformed
3.
Ketik script berikut (untuk mengaktifkan tombol
tambah):
/*
* To
change this template, choose Tools | Templates
* and
open the template in the editor.
*/
/*
*
JFrameMhsTambah.java
*
*
Created on Aug 27, 2012, 7:51:28 PM
*/
package PackageMhsTambah;
import java.sql.*;
import javax.swing.*;
/**
*
*
@author komtas
*/
public class JFrameMhsTambah extends
javax.swing.JFrame {
/**
Creates new form JFrameMhsTambah */
public JFrameMhsTambah() {
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">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
txtNo = new javax.swing.JTextField();
txtNama = new javax.swing.JTextField();
btnTambah = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("No");
jLabel2.setText("Nama");
jTextField1.setText("jTextField1");
txtNo.setName(""); // NOI18N
txtNama.setName(""); // NOI18N
btnTambah.setText("Tambah");
btnTambah.setName(""); // NOI18N
btnTambah.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTambahActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnTambah)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, 157,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtNo,
javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtNo,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtNama,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jLabel2)))
.addGap(28, 28, 28)
.addComponent(btnTambah)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}//
</editor-fold>
private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String no = txtNo.getText();
String nama = txtNama.getText();
try
{
String dbname="jdbc:odbc:DsMahasiswa2";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection(dbname,"komar","komar");
Statement statement = con.createStatement();
String sql="insert into TableMahasiswa
values('"+no+"','"+nama+"');";
statement.executeUpdate(sql);
statement.close();
JOptionPane.showMessageDialog(null, "Data
berhasil dimasukkan..","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());
}
}
/**
*
@param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameMhsTambah().setVisible(true);
}
});
}
//
Variables declaration - do not modify
private javax.swing.JButton btnTambah;
private javax.swing.JLabel jLabel1;
private
javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField txtNama;
private javax.swing.JTextField txtNo;
//
End of variables declaration
}
4.
Simpan
5.
Jalankan program (Run – Run Project)
6.
Akan tampil sebuah form seperti berikut:
7.
Entri data berikut:
No
|
Nama
|
101
|
Cendekia Nagara
|
102
|
Fatahillah
|
103
|
Ahmad
|
8.
Lihat TableMahasiswa yang sudah dibuat pada
Microsoft Access untuk memastikan bahwa data yang dientri sudah tersimpan di
database.
maksih tutorialnya
BalasHapusGrub Bloger Indonesia di Facebook
Pengertian Inheritance Beserta Contoh Programnya
Penjelasan Mengenai Array Dan Contoh Programnya