Mengkoneksikan database dengan java, yaitu database MySql dan Microsoft Access
Kodenya adalah sebagai berikut :
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CobaDatabase {
public static void main (String[] args) {
DBMahasiswa mahasiswa = new DBMahasiswa();
}
}
class DBMahasiswa extends JFrame implements ActionListener {
final JLabel lNIM, lNama, lAlamat;
final JTextField nim, nama, alamat;
final JButton btnSave, btnHapus;
final JPanel panel1, panel2;
public DBMahasiswa() {
setTitle("Coba Database");
lNIM=new JLabel("NIM : ");
lNama=new JLabel("NAMA : ");
lAlamat=new JLabel("ALAMAT : ");
nim = new JTextField(20);
nama= new JTextField(20);
alamat= new JTextField(20);
btnSave=new JButton("SIMPAN");
btnSave.addActionListener(this);
btnHapus=new JButton("HAPUS");
btnHapus.addActionListener(this);
panel1 = new JPanel (new GridLayout (3,1));
panel2 = new JPanel (new GridLayout (3,1));
setLayout(new BorderLayout());
add(panel1, "West");
panel1.add(lNIM);
panel1.add(lNama);
panel1.add(lAlamat);
add(panel2, "East");
panel2.add(nim);
panel2.add(nama);
panel2.add(alamat);
add(btnSave, "South");
add(btnHapus, "North");
pack();
setDefaultCloseOperation(3);
setVisible(true);
}
public void actionPerformed (ActionEvent ae) {
if(ae.getSource()==btnSave) {
String sql = "insert into mahasiswa values('"+nim.getText()+"', '"+nama.getText()+"', '"+alamat.getText()+"')";
try {
//Kode untuk database microsoft access
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/praktikum", "root", "");
//Kode untuk database microsoft access
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mi85", "", "");*/
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
JOptionPane.showMessageDialog(this, "Data berhasil disimpan", "Hasil", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "HAsil", JOptionPane.ERROR_MESSAGE);
}
}
if(ae.getSource()==btnHapus) {
int pesan = JOptionPane.showConfirmDialog(null, "Anda yakin ingin menghapus pegawai\n"+ "dengan NIM ='"+nim.getText()+"'","Konfirmasi Hapus Data",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(pesan == JOptionPane.OK_OPTION) {
String sql = "DELETE from mahasiswa where nim='"+nim.getText()+"'";
try {
//Kode untuk database mysql
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/praktikum", "root", "");
//Kode untuk database microsoft access
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mi85", "", "");*/
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
JOptionPane.showMessageDialog(this, "Data berhasil dihapus", "Hasil", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e ) {
JOptionPane.showMessageDialog(this, e.getMessage(), "Hasil", JOptionPane.ERROR_MESSAGE);
}
}
}
System.exit(0);
}
}
*ada software yang kita tambahkan ya itu mysql-connector-java-5.jar karena kita memakai database mysql
kita letakkan software tersebut di C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext
setelah di buat baru di jalankan...
ini lah hasilnya...
File yang kedua ya itu melihat isi dari database...
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LihatDatabase {
public static void main (String [] args) {
LihatData data = new LihatData ();
}
}
class LihatData extends JFrame {
public LihatData(){
setTitle("Lihat Database");
String tdata[] []=new String[0] [0];
try {
//untuk mysql
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//untuk microsoft access
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mi85", "", "");*/
Statement stmt=conn.createStatement();
String sql="select * from mahasiswa";
ResultSet rs=stmt.executeQuery(sql);
tdata=new String[500][3];
int p=0;
while(rs.next()) {
tdata[p][0] = rs.getString(1);
tdata[p][1] = rs.getString(2);
tdata[p][2] = rs.getString(3);
p++;
}
stmt.close();
conn.close();
}
catch (Exception exc) {
JOptionPane.showMessageDialog(this ,"ERROR", "Hasil", JOptionPane.ERROR_MESSAGE);
}
String[] nkolom={"NIM", "Nama", "Alamat"};
JTable tabel = new JTable(tdata, nkolom);
JScrollPane gridtabel = new JScrollPane(tabel);
tabel.setPreferredScrollableViewportSize(new Dimension(400,300));
setLayout(new FlowLayout());
add(gridtabel);
pack();
setDefaultCloseOperation(3);
setVisible(true);
}
}
dan hasilnya adalah sebagai berikut :
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LihatDatabase {
public static void main (String [] args) {
LihatData data = new LihatData ();
}
}
class LihatData extends JFrame {
public LihatData(){
setTitle("Lihat Database");
String tdata[] []=new String[0] [0];
try {
//untuk mysql
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//untuk microsoft access
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mi85", "", "");*/
Statement stmt=conn.createStatement();
String sql="select * from mahasiswa";
ResultSet rs=stmt.executeQuery(sql);
tdata=new String[500][3];
int p=0;
while(rs.next()) {
tdata[p][0] = rs.getString(1);
tdata[p][1] = rs.getString(2);
tdata[p][2] = rs.getString(3);
p++;
}
stmt.close();
conn.close();
}
catch (Exception exc) {
JOptionPane.showMessageDialog(this ,"ERROR", "Hasil", JOptionPane.ERROR_MESSAGE);
}
String[] nkolom={"NIM", "Nama", "Alamat"};
JTable tabel = new JTable(tdata, nkolom);
JScrollPane gridtabel = new JScrollPane(tabel);
tabel.setPreferredScrollableViewportSize(new Dimension(400,300));
setLayout(new FlowLayout());
add(gridtabel);
pack();
setDefaultCloseOperation(3);
setVisible(true);
}
}
dan hasilnya adalah sebagai berikut :
Tidak ada komentar:
Posting Komentar