import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class BacaDataBos extends JFrame{
Connection konek;
JScrollPane panelGulung = new JScrollPane();
JTable tabel = new JTable();
DefaultTableModel modeleTabel;
public BacaDataBos(){
super("Baca Data dari Database Server");
inisialisasiGUI();
}
public static void main (String args[]){
BacaDataBos data = new BacaDataBos();
data.setVisible(true);
}
public void inisialisasiGUI(){
int lebarJendela = 400;
int tinggiJendela = 450;
Dimension Layar = Toolkit.getDefaultToolkit().getScreenSize();
int awalJendX = (Layar.width-lebarJendela)/2;
int awalJendY = (Layar.height-tinggiJendela)/2;
setSize(lebarJendela,tinggiJendela);
setLocation(awalJendX,awalJendY);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
JButton ambil = new JButton(" AMBIL ");
ambil.setLocation(110,180);
ambil.setSize(200,40);
add(ambil);
Object[] baris = {"Nim", "Nama", "Nomor HP"};
modeleTabel = new DefaultTableModel(null, baris);
panelGulung.setBounds(new Rectangle(15,225,365,175));
tabel.setModel(modeleTabel);
panelGulung.getViewport().add(tabel);
add(panelGulung);
ambil.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
aksitombol();
}
});
koneksiDatabase();
}
public void koneksiDatabase (){
String namadb = "pangkalan";
String usr = "nanungnurzula";
String psw = "masuksurga";
try{
Class.forName("org.postgresql.Driver");
System.out.println("Drivernya sudah ketemu.");
try{
konek = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+namadb, usr, psw);
/*getConnection("jdbc:postgresql://localhost:5432/pangkalan", "nanungnurzula", "masuksurga");*/
/*getConnection("jdbc:postgresql://127.0.0.1/pangkalan?user=nanungnurzula&password=masuksurga");*/
/*getConnection("jdbc:postgresql://localhost:5432/pangkalan?user=nanungnurzula&password=masuksurga"); */
System.out.println("Koneksi berhasil.");
}catch(SQLException sqle){
System.out.println(sqle);
System.out.println("Koneksi gagal.");
System.exit(0);
}
}catch(ClassNotFoundException cnfe){
System.out.println(cnfe);
System.out.println("Drivernya belum ketemu.");
}
}
public void aksitombol(){
hapusIsiTabelYangAda();
pindahkanTabeldiServerKeTabeldiLayar();
}
public void hapusIsiTabelYangAda(){
int jmlBaris = modeleTabel.getRowCount();
for (int i=0; i<jmlBaris; i++){
modeleTabel.removeRow(0);
}
}
public void pindahkanTabeldiServerKeTabeldiLayar(){
try {
String SQLe = "select * from databos";
Statement perintah = konek.createStatement();
ResultSet hasile = perintah.executeQuery(SQLe);
while (hasile.next()) {
String nime = hasile.getString("nim");
String namane = hasile.getString("nama");
String nohpne = hasile.getString("nohp");
String[] dataYgDidapat = {nime,namane,nohpne};
modeleTabel.addRow(dataYgDidapat);
}
}catch( Exception e) {/* kalo salah dicuekin */}
}
}
Tuesday, January 1, 2013
BacaDataBos.java AWT
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment