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 EditDataBos extends JFrame{
7: JLabel lNim = new JLabel("NIM");
8: JLabel lNama = new JLabel("Nama");
9: JLabel lNohp = new JLabel("No HP");
10: JLabel lKec = new JLabel("Kecamatan");
11: JLabel lKab = new JLabel("Kabupaten");
12: JTextField tNim = new JTextField();
13: JTextField tNama = new JTextField();
14: JTextField tNohp = new JTextField();
15: JTextField tKec = new JTextField();
16: JTextField tKab = new JTextField();
17: Connection konek;
18: JScrollPane panelGulung = new JScrollPane();
19: JTable tabel = new JTable() {
20: public boolean isCellEditable(int row, int column){
21: return false;
22: }
23: };
24: DefaultTableModel modeleTabel;
25: public EditDataBos(){
26: super("Melengkapi Data Bos-Bos");
27: inisialisasiGUI();
28: }
29: public static void main (String args[]){
30: EditDataBos data = new EditDataBos();
31: data.setVisible(true);
32: }
33: public void inisialisasiGUI(){
34: int lebarJendela = 600;
35: int tinggiJendela = 600;
36: Dimension Layar = Toolkit.getDefaultToolkit().getScreenSize();
37: int awalJendX = (Layar.width-lebarJendela)/2;
38: int awalJendY = (Layar.height-tinggiJendela)/2;
39: setSize(lebarJendela,tinggiJendela);
40: setLocation(awalJendX,awalJendY);
41: setResizable(false);
42: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43: this.setLayout(null);
44: lNim.setLocation(110,60);
45: lNim.setSize(60,25);
46: tNim.setLocation(220,60);
47: tNim.setSize(150,25);
48: tNim.setFocusable(false);
49: lNama.setLocation(110,90);
50: lNama.setSize(60,25);
51: tNama.setLocation(220,90);
52: tNama.setSize(150,25);
53: tNama.setFocusable(false);
54: lNohp.setLocation(110,120);
55: lNohp.setSize(60,25);
56: tNohp.setLocation(220,120);
57: tNohp.setSize(150,25);
58: tNohp.setFocusable(false);
59: lKec.setLocation(110,150);
60: lKec.setSize(110,25);
61: tKec.setLocation(220,150);
62: tKec.setSize(150,25);
63: lKab.setLocation(110,180);
64: lKab.setSize(110,25);
65: tKab.setLocation(220,180);
66: tKab.setSize(150,25);
67: JButton hapus = new JButton(" Ganti ");
68: hapus.setLocation(110,240);
69: hapus.setSize(260,40);
70: Object[] baris = {"Nim", "Nama", "Nomor HP","Kecamatan","Kabupaten"};
71: modeleTabel = new DefaultTableModel(null, baris);
72: panelGulung.setBounds(new Rectangle(15,325,565,225));
73: tabel.setModel(modeleTabel);
74: panelGulung.getViewport().add(tabel);
75: add(panelGulung);
76: tabel.addMouseListener(new MouseAdapter() {
77: // Proses saat TABEL di klik
78: public void mouseClicked(MouseEvent e) {
79: dariTabeldiLayarkeTextField();
80: }
81: });
82: add(lNim);
83: add(tNim);
84: add(lNama);
85: add(tNama);
86: add(lNohp);
87: add(tNohp);
88: add(lKec);
89: add(tKec);
90: add(lKab);
91: add(tKab);
92: add(hapus);
93: hapus.addActionListener( new ActionListener(){
94: public void actionPerformed(ActionEvent e) {
95: aksitombol();
96: }
97: });
98: koneksiDatabase();
99: pindahkanTabeldiServerKeTabeldiLayar();
100: }
101: public void aksitombol(){
102: prosesHapusData();
103: }
104: public void koneksiDatabase (){
105: try{
106: Class.forName("com.mysql.jdbc.Driver");
107: System.out.println("Drivernya sudah ketemu.");
108: try{
109: konek = DriverManager.
110: getConnection("jdbc:mysql://localhost:3306/mondaypahing?user=root&password=");
111: System.out.println("Koneksi berhasil.");
112: }catch(SQLException sqle){
113: System.out.println(sqle);
114: System.out.println("Koneksi gagal.");
115: System.exit(0);
116: }
117: }catch(ClassNotFoundException cnfe){
118: System.out.println(cnfe);
119: System.out.println("Drivernya belum ketemu.");
120: }
121: }
122: public void pindahkanTabeldiServerKeTabeldiLayar(){
123: try {
124: String SQLe = "select * from databos";
125: Statement perintah = konek.createStatement();
126: ResultSet hasile = perintah.executeQuery(SQLe);
127: while (hasile.next()) {
128: String nime = hasile.getString("nim");
129: String namane = hasile.getString("nama");
130: String nohpne = hasile.getString("nohp");
131: String kecane = hasile.getString("kecamatan");
132: String kabune = hasile.getString("kabupaten");
133: String[] dataYgDidapat = {nime,namane,nohpne,kecane,kabune};
134: modeleTabel.addRow(dataYgDidapat);
135: }
136: }catch( Exception e) {/* kalo salah dicuekin */}
137: }
138: public void dariTabeldiLayarkeTextField() {
139: tNim.setText("");
140: tNama.setText("");
141: tNohp.setText("");
142: tKec.setText("");
143: tKab.setText("");
144: int baris = tabel.getSelectedRow();
145: tNim.setText(modeleTabel.getValueAt(baris,0).toString());
146: tNama.setText(modeleTabel.getValueAt(baris,1).toString());
147: tNohp.setText(modeleTabel.getValueAt(baris,2).toString());
148: tKec.setText(modeleTabel.getValueAt(baris,3).toString());
149: tKab.setText(modeleTabel.getValueAt(baris,4).toString());
150: }
151: public void hapusIsiTabelYangAda(){
152: int jmlBaris = modeleTabel.getRowCount();
153: for (int i=0; i<jmlBaris; i++){
154: modeleTabel.removeRow(0);
155: }
156: }
157: public void prosesHapusData(){
158: try {
159: String SQLe = "update databos set kecamatan = ?, kabupaten = ? WHERE nim ='"+tNim.getText()+"'";
160: PreparedStatement stat = konek.prepareStatement(SQLe);
161: stat.setString(1,tKec.getText());
162: stat.setString(2,tKab.getText());
163: stat.executeUpdate();
164: hapusIsiTabelYangAda();
165: pindahkanTabeldiServerKeTabeldiLayar();
166: tNim.setText("");
167: tNama.setText("");
168: tNohp.setText("");
169: tKec.setText("");
170: tKab.setText("");
171: }catch(SQLException se){/* kalo salah dicuekin */}
172: }
173: }
Friday, July 19, 2013
EditDataBos.java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment