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:mi90", "", "");
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 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 con = DriverManager.getConnection("jdbc:mysql://localhost/praktikum", "root", "");*/
//Untuk database ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mi90", "", "");
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);
}
}
hasil program
2. Program melihat 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 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:mi90", "", "");
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 dibebaskan dari memori
stmt.close();
//koneksi ditutp
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);
}
}
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:mi90", "", "");
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 dibebaskan dari memori
stmt.close();
//koneksi ditutp
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);
}
}
hasil program
Tidak ada komentar:
Posting Komentar