Menampilkan Data pada Tabel | Java Netbeans
Yappp.... seperti judulnya ini kodingan untuk menampilkan data pada
database yang udah dibikin ke dalam bentuk tabel. Cekidot gann.... ^o^
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Jie
*/
public class DBConnection {
private Connection koneksi;
public Connection connect(){
//untuk koneksi ke driver
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("berhasil load driver");
}catch(ClassNotFoundException cnfe){
System.out.println("Tidak ada Driver "+cnfe);
}
//untuk koneksi ke database
try{
String url="jdbc:mysql://localhost:3306/java_db";
koneksi=DriverManager.getConnection(url,"root","root");
System.out.println("Berhasil koneksi");
}catch(SQLException se){
System.out.println("Gagal koneksi "+se);
//JOptionPane.showMessageDialog(null,"Gagal koneksi...MySQL server belum aktif");
}
return koneksi;
}
}
#Kodingannya
import Config.DBConnection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Form.java
*
* Created on 28 Des 11, 7:37:11
*/
/**
*
* @author Jie
*/
public class Utama extends javax.swing.JFrame {
private DefaultTableModel DftTabMode;
/** Creates new form Form */
public Utama() {
initComponents();
setLocationRelativeTo(this);
}
/** 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() {
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
JFrame form = new JFrame();
form.setSize(400, 400);
tampil_data();
}
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
//saat klik record
klik_record();
}
private void updateMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
update_data();
}
private void deleteMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
delete_data();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
insert_data();
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Utama().setVisible(true);
}
});
}
////////////////////////////untuk metod baru ////////////////////////////////////
public void tampil_data(){
//panggil method koneksi
java.sql.Connection conn = new DBConnection().connect();
Object[] Baris={"ID","Nama","Tanggal Lahir","Alamat"};
DftTabMode=new DefaultTableModel(null,Baris);
jTable1.setModel(DftTabMode);
try {
String sql = "Select * from biodata";
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rslt = stmt.executeQuery(sql);
while (rslt.next()) {
String id = rslt.getString("id");
String nama = rslt.getString("nama");
String tanggal = rslt.getString("tanggal");
String alamat = rslt.getString("alamat");
String[] dataField = {id, nama, tanggal, alamat};
DftTabMode.addRow(dataField);
}
}catch(Exception e) {}
}
public void klik_record(){
//saat pilih record
int baris = jTable1.getSelectedRow();
String id = DftTabMode.getValueAt(baris, 0).toString();
String nama = DftTabMode.getValueAt(baris, 1).toString();
String tanggal = DftTabMode.getValueAt(baris, 2).toString();
String alamat = DftTabMode.getValueAt(baris, 3).toString();
f_id.setText(id);
f_nama.setText(nama);
f_tanggal.setText(tanggal);
f_alamat.setText(alamat);
}
public void update_data(){
//panggil method koneksi
java.sql.Connection conn = new DBConnection().connect();
String sql="update biodata set nama=?,tanggal=?,alamat=? where id='"+f_id.getText()+"'";
java.sql.PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException ex) {
}
try{
stmt.setString(1,f_nama.getText());
stmt.setString(2,f_tanggal.getText());
stmt.setString(3,f_alamat.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Data Berhasil di Ubah");
}catch(SQLException se){}
}
public void insert_data(){
//panggil method koneksi
java.sql.Connection conn = new DBConnection().connect();
String sql="INSERT INTO `java_db`.`biodata` (`id`, `nama`, `tanggal`, `alamat`) VALUES (?, ?, ?, ?);";
java.sql.PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException ex) {
}
try{
stmt.setString(1, f_id.getText());
stmt.setString(2,f_nama.getText());
stmt.setString(3,f_tanggal.getText());
stmt.setString(4,f_alamat.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Data Berhasil di Ditambah");
}catch(SQLException se){}
}
public void delete_data(){
//panggil method koneksi
java.sql.Connection conn = new DBConnection().connect();
int ok=JOptionPane.showConfirmDialog(null,"Apakah Anda yakin?","Konfirmasi",JOptionPane.YES_NO_OPTION);
if(ok==0){
try{
String sql="delete from biodata where id='"+f_id.getText()+"'";
java.sql.PreparedStatement stmt=conn.prepareStatement(sql);
stmt.executeUpdate();
JOptionPane.showMessageDialog(null,"Data Berhasil di hapus");
f_id.setText("");
f_nama.setText("");
f_tanggal.setText("");
f_alamat.setText("");
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Data Gagal di hapus");
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton delete;
private javax.swing.JTextArea f_alamat;
private javax.swing.JTextField f_id;
private javax.swing.JTextField f_nama;
private javax.swing.JTextField f_tanggal;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JButton update;
// End of variables declaration
}
Bingung?? wahahaha... Download aja dimari gan!!
Klik icon Dhe2 Ajah ----> ^o^ <-----
0 komentar:
Posting Komentar