-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyBook.java
356 lines (321 loc) · 16.9 KB
/
myBook.java
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
package book;
import com.mysql.jdbc.*;
import java.sql.DriverManager;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class myBook extends javax.swing.JFrame {
public myBook() {
initComponents();
LoadTable();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
T_author = new javax.swing.JTextField();
T_publisher = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
T_preface = new javax.swing.JTextField();
B_modBtn = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
B_clearBtn = new javax.swing.JButton();
T_accnum = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
T_name = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
B_addBtn = new javax.swing.JButton();
B_deleteBtn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
datatab = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
jLabel9.setText("Publisher");
jLabel10.setText("Preface");
B_modBtn.setText("Modify");
B_modBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B_modBtnActionPerformed(evt);
}
});
jLabel6.setText("Accession Number");
B_clearBtn.setText("Clear");
B_clearBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B_clearBtnActionPerformed(evt);
}
});
jLabel7.setText("Name");
jLabel8.setText("Author");
B_addBtn.setText("Add");
B_addBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B_addBtnActionPerformed(evt);
}
});
B_deleteBtn.setText("Delete");
B_deleteBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B_deleteBtnActionPerformed(evt);
}
});
datatab.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"
}
));
datatab.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
datatabMouseClicked(evt);
}
});
jScrollPane1.setViewportView(datatab);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(B_addBtn)
.addGap(18, 18, 18)
.addComponent(B_deleteBtn)
.addGap(18, 18, 18)
.addComponent(B_modBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(B_clearBtn))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(T_preface, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(T_publisher, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel6)
.addGap(0, 4, Short.MAX_VALUE)))
.addGap(41, 41, 41)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(T_name)
.addComponent(T_author)
.addComponent(T_accnum, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(106, 106, 106))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(T_accnum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(T_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(T_author, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(T_publisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel10)
.addComponent(T_preface, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(B_addBtn)
.addComponent(B_deleteBtn)
.addComponent(B_modBtn)
.addComponent(B_clearBtn))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void B_addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_addBtnActionPerformed
String name,author,publisher,preface;
String acc_num,query;
acc_num=T_accnum.getText();
name=T_name.getText();
author=T_author.getText();
publisher=T_publisher.getText();
preface=T_preface.getText();
query = "insert into book values ("+acc_num+",'"+name+"','"+author+"','"+publisher+"','"+preface+"')";
connection(query);
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
LoadTable();
}//GEN-LAST:event_B_addBtnActionPerformed
private void B_clearBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_clearBtnActionPerformed
T_accnum.setText("");
T_author.setText("");
T_name.setText("");
T_preface.setText("");
T_publisher.setText("");
}//GEN-LAST:event_B_clearBtnActionPerformed
private void datatabMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_datatabMouseClicked
DefaultTableModel model = (DefaultTableModel)datatab.getModel();
int selectedRowIndex = datatab.getSelectedRow();
String acnum = model.getValueAt(selectedRowIndex, 0).toString();
T_accnum.setText(acnum);
String bname = model.getValueAt(selectedRowIndex, 1).toString();
T_name.setText(bname);
String bauthor = model.getValueAt(selectedRowIndex, 2).toString();
T_author.setText(bauthor);
String bpub = model.getValueAt(selectedRowIndex, 3).toString();
T_publisher.setText(bpub);
String bpreface = model.getValueAt(selectedRowIndex, 4).toString();
T_preface.setText(bpreface);
}//GEN-LAST:event_datatabMouseClicked
private void B_modBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_modBtnActionPerformed
String name,author,publisher,preface;
String acc_num;
Connection conn = null;
PreparedStatement stmt;
ResultSet rs;
acc_num=T_accnum.getText();
name=T_name.getText();
author=T_author.getText();
publisher=T_publisher.getText();
preface=T_preface.getText();
JOptionPane.showMessageDialog(null,"Updated");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root", "root");
String queryI = "UPDATE book SET book_name = '"+name+"',book_author = '"+author+"',book_publisher = '"+publisher+"',book_preface = '"+preface+"' where accession_number = "+acc_num+"";
stmt = (PreparedStatement) conn.prepareStatement(queryI);
if(conn!=null)
System.out.println("Connected");
stmt.executeUpdate(queryI);
} catch (Exception ex) {
System.out.println("Not Connected");
}
LoadTable();
}//GEN-LAST:event_B_modBtnActionPerformed
private void B_deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_deleteBtnActionPerformed
String acc_num;
Connection conn = null;
PreparedStatement stmt;
ResultSet rs;
acc_num=T_accnum.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root", "root");
String queryI = "delete from book where accession_number = '"+acc_num+"' ";
stmt = (PreparedStatement) conn.prepareStatement(queryI);
if(conn!=null)
System.out.println("Connected");
stmt.executeUpdate(queryI);
} catch (Exception ex) {
System.out.println("Not Connected");
}
JOptionPane.showMessageDialog(null,"Deleted");
LoadTable();
}//GEN-LAST:event_B_deleteBtnActionPerformed
public void connection(String query) {
Connection conn = null;
Statement stmt;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root", "root");
stmt = (Statement) conn.createStatement();
if(conn!=null)
System.out.println("Connected");
stmt.executeUpdate(query);
} catch (Exception ex) {
System.out.println("Not Connected");
}
}
public void LoadTable()
{
Statement stmt;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root", "root");
stmt = (Statement) conn.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery("select * from book");
Vector head=new Vector();
head.addElement("Acc No");
head.addElement("Name");
head.addElement("Author");
head.addElement("Publisher");
head.addElement("Preface");
Vector data=new Vector();
while(rs.next())
{
Vector row=new Vector();
for(int i=1;i<=5;i++)
row.addElement(rs.getString(i));
data.addElement(row);
}
DefaultTableModel model=new DefaultTableModel(data, head);
datatab.setModel(model);
//model.getDataVector().elementAt(datatab.getSelectedRow());
if(conn!=null)
System.out.println("Connected");
} catch (Exception ex) {
System.out.println("Not Connected"+ex);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new myBook().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton B_addBtn;
private javax.swing.JButton B_clearBtn;
private javax.swing.JButton B_deleteBtn;
private javax.swing.JButton B_modBtn;
private javax.swing.JTextField T_accnum;
private javax.swing.JTextField T_author;
private javax.swing.JTextField T_name;
private javax.swing.JTextField T_preface;
private javax.swing.JTextField T_publisher;
private javax.swing.JTable datatab;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}