-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab 9.txt
More file actions
439 lines (407 loc) · 20.8 KB
/
Copy pathlab 9.txt
File metadata and controls
439 lines (407 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package com.mycompany.mavenproject2;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class OneG extends javax.swing.JFrame {
/**
* Creates new form OneG
*/
public OneG() {
initComponents();
connect();
Load();
}
Connection con;
PreparedStatement pat;
DefaultTableModel def;
public void connect()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/Student","root","");
} catch (ClassNotFoundException ex) {
Logger.getLogger(OneG.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(OneG.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void Load()
{
try {
pat=con.prepareStatement("select *from inter");
ResultSet rs=pat.executeQuery();
ResultSetMetaData rss=rs.getMetaData();
int c;
c=rss.getColumnCount();
def=(DefaultTableModel)jTable3.getModel();
def.setRowCount(0);
while(rs.next())
{
Vector v=new Vector();
for(int i=0;i<c;i++)
{
v.add(rs.getString("id"));
v.add(rs.getString("name"));
v.add(rs.getString("contact"));
v.add(rs.getString("college"));
v.add(rs.getString("course"));
}
def.addRow(v);
}
} catch (SQLException ex) {
Logger.getLogger(OneG.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
jTable2 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
txtName = new javax.swing.JTextField();
txtContact = new javax.swing.JTextField();
txtCollege = new javax.swing.JTextField();
txtCourse = new javax.swing.JTextField();
add_b = new javax.swing.JButton();
update_b = new javax.swing.JButton();
search_b = new javax.swing.JButton();
delete_b = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jScrollPane3 = new javax.swing.JScrollPane();
jTable3 = new javax.swing.JTable();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
jTable2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable2);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
jLabel2.setText("Contact");
jLabel3.setText("College");
jLabel4.setText("Course");
txtCollege.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCollegeActionPerformed(evt);
}
});
add_b.setText("add");
add_b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
add_bActionPerformed(evt);
}
});
update_b.setText("update");
update_b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
update_bActionPerformed(evt);
}
});
search_b.setText("search");
search_b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
search_bActionPerformed(evt);
}
});
delete_b.setText("delete");
delete_b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delete_bActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 239, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 51, Short.MAX_VALUE)
);
jTable3.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null}
},
new String [] {
"id", "name", "contact", "college", "course"
}
));
jTable3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable3MouseClicked(evt);
}
});
jScrollPane3.setViewportView(jTable3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(search_b)
.addComponent(add_b)))
.addGroup(layout.createSequentialGroup()
.addGap(4, 4, 4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(update_b)
.addComponent(delete_b)
.addComponent(txtCollege, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtContact, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtCollege, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(txtCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(update_b)
.addComponent(add_b))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(search_b)
.addComponent(delete_b))
.addGap(0, 56, Short.MAX_VALUE))
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void txtCollegeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void add_bActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String name,contact,college,course;
name=txtName.getText();
contact=txtContact.getText();
college=txtCollege.getText();
course=txtCourse.getText();
pat=con.prepareStatement("insert into inter(name,contact,college,course)values(?,?,?,?)");
pat.setString(1,name);
pat.setString(2,contact);
pat.setString(3,college);
pat.setString(4,course);
pat.executeUpdate();
JOptionPane.showMessageDialog(this,"Records Added");
Load();
txtName.setText("");
txtContact.setText("");
txtCollege.setText("");
txtCourse.setText("");
txtName.requestFocus();
} catch (SQLException ex) {
Logger.getLogger(OneG.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void update_bActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
def=(DefaultTableModel)jTable3.getModel();
int selected=jTable3.getSelectedRow();
int id=Integer.parseInt(def.getValueAt(selected, 0).toString());
try {
String name,contact,college,course;
name=txtName.getText();
contact=txtContact.getText();
college=txtCollege.getText();
course=txtCourse.getText();
pat=con.prepareStatement("update inter set name=?,contact=?,college=?,course=? where id=?");
pat.setString(1,name);
pat.setString(2,contact);
pat.setString(3,college);
pat.setString(4,course);
pat.setInt(5, id);
pat.executeUpdate();
JOptionPane.showMessageDialog(this,"Records Updated");
Load();
txtName.setText("");
txtContact.setText("");
txtCollege.setText("");
txtCourse.setText("");
txtName.requestFocus();
} catch (SQLException ex) {
Logger.getLogger(oneD.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void search_bActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
def=(DefaultTableModel)jTable3.getModel();
int rows = jTable3.getRowCount();
boolean record=false;
String name1 =txtName.getText();
for(int i=0;i<rows;i++){
String n = (String) jTable3.getValueAt(i, 1);
if(name1.equals(n)){
txtName.setText(def.getValueAt(i, 1).toString());
txtContact.setText(def.getValueAt(i, 2).toString());
txtCollege.setText(def.getValueAt(i, 3).toString());
txtCourse.setText(def.getValueAt(i, 4).toString());
int ID=Integer.parseInt(def.getValueAt(i, 0).toString());
JOptionPane.showMessageDialog(this,"Record found at"+ID+".");
record=true;
break;
}
}
if(!record){
JOptionPane.showMessageDialog(this,"No Records Found");
}
}
private void delete_bActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
def=(DefaultTableModel)jTable3.getModel();
int selected=jTable1.getSelectedRow();
int id=Integer.parseInt(def.getValueAt(selected, 0).toString());
try {
pat=con.prepareStatement("delete from inter where id=?");
pat.setInt(1, id);
pat.executeUpdate();
JOptionPane.showMessageDialog(this,"Records Deleted");
Load();
txtName.setText("");
txtContact.setText("");
txtCollege.setText("");
txtCourse.setText("");
txtName.requestFocus();
} catch (SQLException ex) {
Logger.getLogger(oneD.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jTable3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
def=(DefaultTableModel)jTable3.getModel();
int selected=jTable3.getSelectedRow();
int id=Integer.parseInt(def.getValueAt(selected, 0).toString());
txtName.setText(def.getValueAt(selected, 1).toString());
txtContact.setText(def.getValueAt(selected, 2).toString());
txtCollege.setText(def.getValueAt(selected, 3).toString());
txtCourse.setText(def.getValueAt(selected, 4).toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(OneG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OneG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OneG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OneG.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OneG().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton add_b;
private javax.swing.JButton delete_b;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable jTable3;
private javax.swing.JButton search_b;
private javax.swing.JTextField txtCollege;
private javax.swing.JTextField txtContact;
private javax.swing.JTextField txtCourse;
private javax.swing.JTextField txtName;
private javax.swing.JButton update_b;
// End of variables declaration
}