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
MasukDataBos.java AWT
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class MasukDataBos extends JFrame{
JLabel lNim = new JLabel("NIM");
JLabel lNama = new JLabel("Nama");
JLabel lNohp = new JLabel("No HP");
JTextField tNim = new JTextField();
JTextField tNama = new JTextField();
JTextField tNohp = new JTextField();
Connection konek;
public MasukDataBos(){
super("Memasukan Data Bos-Bos");
inisialisasiGUI();
}
public static void main (String args[]){
MasukDataBos data = new MasukDataBos();
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);
lNim.setLocation(110,60);
lNim.setSize(60,25);
tNim.setLocation(160,60);
tNim.setSize(150,25);
lNama.setLocation(110,90);
lNama.setSize(60,25);
tNama.setLocation(160,90);
tNama.setSize(150,25);
lNohp.setLocation(110,120);
lNohp.setSize(60,25);
tNohp.setLocation(160,120);
tNohp.setSize(150,25);
JButton masuk = new JButton(" Masuk ");
masuk.setLocation(110,180);
masuk.setSize(200,40);
add(lNim);
add(tNim);
add(lNama);
add(tNama);
add(lNohp);
add(tNohp);
add(masuk);
masuk.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
aksitombol();
}
});
koneksiDatabase();
}
public void aksitombol(){
try{
String sql = "Insert Into databos values(?,?,?)";
PreparedStatement stat = konek.prepareStatement(sql);
try{
stat.setString(1,tNim.getText());
stat.setString(2,tNama.getText());
stat.setString(3,tNohp.getText());
stat.executeUpdate();
tNim.setText("");
tNama.setText("");
tNohp.setText("");
}catch(SQLException se){
System.out.println(se);
}
}catch(Exception e){
System.out.println(e);
}
}
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.");
}
}
}
Subscribe to:
Posts (Atom)