Laman

Friday, July 19, 2013

HapusDataBos1.java

1:  import java.awt.*;  
2:  import java.awt.event.*;  
3:  import java.sql.*;  
4:  import javax.swing.*;  
5:  import javax.swing.table.*;  
6:  public class HapusDataBos1 extends JFrame{  
7:     JLabel lNim  = new JLabel("NIM");  
8:     JLabel lNama = new JLabel("Nama");  
9:     JLabel lNohp = new JLabel("No HP");  
10:     JTextField tNim  = new JTextField();  
11:     JTextField tNama = new JTextField();   
12:     JTextField tNohp = new JTextField();  
13:     Connection konek;  
14:     JScrollPane panelGulung = new JScrollPane();  
15:     JTable tabel = new JTable();  
16:     DefaultTableModel modeleTabel;  
17:   public HapusDataBos1(){  
18:     super("Menghapus Data Bos-Bos");  
19:     inisialisasiGUI();  
20:   }  
21:   public static void main (String args[]){  
22:     HapusDataBos1 data = new HapusDataBos1();  
23:     data.setVisible(true);  
24:   }  
25:   public void inisialisasiGUI(){  
26:     int lebarJendela = 400;  
27:     int tinggiJendela = 450;  
28:     Dimension Layar = Toolkit.getDefaultToolkit().getScreenSize();  
29:     int awalJendX = (Layar.width-lebarJendela)/2;  
30:     int awalJendY = (Layar.height-tinggiJendela)/2;  
31:     setSize(lebarJendela,tinggiJendela);  
32:     setLocation(awalJendX,awalJendY);  
33:     setResizable(false);  
34:     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
35:     this.setLayout(null);  
36:     lNim.setLocation(110,60);  
37:     lNim.setSize(60,25);  
38:     tNim.setLocation(160,60);  
39:     tNim.setSize(150,25);  
40:     lNama.setLocation(110,90);  
41:     lNama.setSize(60,25);  
42:     tNama.setLocation(160,90);  
43:     tNama.setSize(150,25);  
44:     lNohp.setLocation(110,120);  
45:     lNohp.setSize(60,25);  
46:     tNohp.setLocation(160,120);  
47:     tNohp.setSize(150,25);  
48:     JButton hapus = new JButton(" Hapus ");  
49:     hapus.setLocation(110,180);  
50:     hapus.setSize(200,40);  
51:     Object[] baris = {"Nim", "Nama", "Nomor HP"};  
52:     modeleTabel = new DefaultTableModel(null, baris);  
53:     panelGulung.setBounds(new Rectangle(15,225,365,175));  
54:     tabel.setModel(modeleTabel);  
55:     panelGulung.getViewport().add(tabel);  
56:     add(panelGulung);  
57:     tabel.addMouseListener(new MouseAdapter() {  
58:     // Proses saat TABEL di klik  
59:     public void mouseClicked(MouseEvent e) {  
60:      dariTabeldiLayarkeTextField();  
61:     }  
62:    });  
63:     add(lNim);  
64:     add(tNim);  
65:     add(lNama);  
66:     add(tNama);  
67:     add(lNohp);  
68:     add(tNohp);  
69:     add(hapus);  
70:     hapus.addActionListener( new ActionListener(){  
71:      public void actionPerformed(ActionEvent e) {  
72:       aksitombol();  
73:      }  
74:     });  
75:     koneksiDatabase();  
76:     pindahkanTabeldiServerKeTabeldiLayar();  
77:     }  
78:     public void aksitombol(){  
79:      prosesHapusData();  
80:     }  
81:     public void koneksiDatabase (){  
82:      try{  
83:       Class.forName("org.postgresql.Driver");  
84:       System.out.println("Drivernya sudah ketemu.");  
85:       try{  
86:        konek = DriverManager.  
87:        /*  getConnection("jdbc:postgresql://127.0.0.1/pangkalan?user=nanungnurzula&password=masuksurga");*/  
88:         getConnection("jdbc:postgresql://localhost:5432/pangkalan?user=nanungnurzula&password=masuksurga");  
89:       System.out.println("Koneksi berhasil.");  
90:      }catch(SQLException sqle){  
91:       System.out.println(sqle);  
92:       System.out.println("Koneksi gagal.");  
93:       System.exit(0);  
94:      }  
95:     }catch(ClassNotFoundException cnfe){  
96:       System.out.println(cnfe);  
97:       System.out.println("Drivernya belum ketemu.");  
98:     }  
99:     }  
100:     public void pindahkanTabeldiServerKeTabeldiLayar(){  
101:     try {  
102:      String SQLe = "select * from databos";  
103:      Statement perintah = konek.createStatement();  
104:      ResultSet hasile = perintah.executeQuery(SQLe);  
105:      while (hasile.next()) {  
106:       String nime  = hasile.getString("nim");  
107:       String namane = hasile.getString("nama");  
108:       String nohpne = hasile.getString("nohp");  
109:       String[] dataYgDidapat = {nime,namane,nohpne};  
110:       modeleTabel.addRow(dataYgDidapat);  
111:      }  
112:     }catch( Exception e) {/* kalo salah dicuekin */}  
113:     }  
114:     public void dariTabeldiLayarkeTextField() {  
115:      int baris = tabel.getSelectedRow();  
116:      tNim.setText(modeleTabel.getValueAt(baris,0).toString());  
117:      tNama.setText(modeleTabel.getValueAt(baris,1).toString());  
118:      tNohp.setText(modeleTabel.getValueAt(baris,2).toString());  
119:     }  
120:     public void hapusIsiTabelYangAda(){  
121:     int jmlBaris = modeleTabel.getRowCount();  
122:     for (int i=0; i<jmlBaris; i++){  
123:      modeleTabel.removeRow(0);  
124:      }  
125:     }  
126:     public void prosesHapusData(){  
127:      try {  
128:       String SQLe = "DELETE FROM databos WHERE nim ='"+tNim.getText()+"'";  
129:       PreparedStatement stat = konek.prepareStatement(SQLe);  
130:       stat.executeUpdate();  
131:       hapusIsiTabelYangAda();  
132:       pindahkanTabeldiServerKeTabeldiLayar();  
133:       tNim.setText("");  
134:       tNama.setText("");  
135:       tNohp.setText("");  
136:       }catch(SQLException se){/* kalo salah dicuekin */}  
137:     }  
138:  }  

No comments:

Post a Comment