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.");
}
}
}
Tuesday, January 1, 2013
MasukDataBos.java AWT
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment