Tugas Pertemuan 14
Untuk database Mysql
CobaDatabase.java
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 {
//untuk database SQL
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//untuk database acess
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDrive");
Connection conn= DriverManager.getConnection("jdbc:odbc:coba","","");*/
Statement stmt = con.createStatement ();
stmt.executeUpdate(sql);
stmt.close();
JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan", "Hasil",JOptionPane.INFORMATION_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 mendhapus 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 {
//untuk database SQl
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//untuk database acess
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDrive");
Connection conn= DriverManager.getConnection("jdbc:odbc:coba","","");*/
Statement stmt = con.createStatement ();
stmt.executeUpdate(sql);
stmt.close();
con.close();
JOptionPane.showMessageDialog(this, "Data Berhasil Disimpan", "Hasil",JOptionPane.INFORMATION_MESSAGE);
}catch(Exception e) {
JOptionPane.showMessageDialog (this, e.getMessage(), "Hasil", JOptionPane.ERROR_MESSAGE);
}
}
}
System.exit(0);
}
}
Jika program tersebut diijalan kan maka akan muncul hasil seperti dibawah ini :
Setelah itu masukkan isi dari nama, nim,dan alamat. seperti contoh dibawah ini :
Jika data berhasil disimpan maka akan muncul hasil seperti dibawah ini :
untuk menghapus data tersebut maka muncul seperti :
LihatDatabase.java
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 Database menggunakan SQL
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//untuk database menggunakan Access
/*Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:coba", "", "");*/
Statement stmt = conn.createStatement();
//Sintaks sql yang dibawa
String sql="select * from mahasiswa ";
//Hasilnya ditampung disini
ResultSet rs = stmt.executeQuery(sql);
tdata = new String [500][3];
int p=0;
//jika data berikutnya ada, maka...
while (rs.next()){
//Kita dapatkan data dari kolom 1
tdata[p] [0] = rs.getString(1);
tdata[p] [1] = rs.getString(2);
tdata[p] [2] = rs.getString(3);
p++;
}
//stmt dibenaskan dari memori
stmt.close();
//koneksi ditutup
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);
}
}
1. Hasil untuk Database Sql
Hasil jika disimpan :
2. Hasil untuk Database Access
Untuk Database Access
Untuk menampilkan data yang ada didatabase access maka kita harus menutup perintah untuk database MySql dan caranya sebagai berikut:
CobaDatabase.java
//Untuk database ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:Coba","","");
//Untuk database SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");*/
LihatDatabase.java
//Untuk Database ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:Coba","","");
//Untuk Database menggunakan SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");*/