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 con =DriverManager.getConnection ("jdbc:mysql://localhost/praktikum", "root", "");
//Untuk database ACCESS
/*Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection ("jdbc:odbc:java", "", "");*/
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);
}
}
if (ae.getSource () ==btnHapus) {
int pesan = JOptionPane.showConfirmDialog (null, "Anda yakin ingin menghapus [egawai\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 con =
DriverManager.getConnection ("jdbc:mysql://localhost/praktikum", "root", "");
//Untuk database ACCESS
/*Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection ("jdbc:odbc:java", "", "");*/
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);
}
}
Jika data dihapus maka tampilannya akan seperti berikut :
kemudian contoh program LihatData beserta outputnya :
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 databse menggunakan SQL
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");
//Untuk database menggunakan ACCES
/*Class.forName("sun.jdsc:odbc.jdbcOdbcDriver");
Connection conn= DriverManager.getConnection (jdbc:odbc:java", "", "") ;*/ Statement stmt = conn.createStatement();
//Sintakx 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 ad 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 dibebaskan 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);
}
}
Post a Comment
FOLLOW ME WILL BE FOLLOW YOU
JANGAN BUANG SPAM SEMBARANGAN LHO ^_^