-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaCurd.java
More file actions
320 lines (273 loc) · 8.76 KB
/
JavaCurd.java
File metadata and controls
320 lines (273 loc) · 8.76 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
import java.awt.EventQueue;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import net.proteanit.sql.DbUtils;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JavaCurd {
private JFrame frame;
private JTextField txtbname;
private JTextField txtedition;
private JTextField txtprice;
private JTable table;
private JTextField txtbid;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaCurd window = new JavaCurd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JavaCurd() {
initialize();
Connect();
table_load();
}
Connection con;
PreparedStatement pst;
ResultSet rs;
public void Connect()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javacurd","root","system");
}
catch (Exception ex)
{
System.out.println("ERROR IN connection");
}
}
public void table_load()
{
try
{
pst = con.prepareStatement("Select * from Book");
rs=pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel (rs));
}
catch (SQLException e)
{
System.out.println("ERROE IN TABLE LOAD");
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 255, 255));
frame.setBounds(100, 100, 885, 662);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Book Shop");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
lblNewLabel.setBounds(346, 10, 241, 62);
frame.getContentPane().add(lblNewLabel);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Registration", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
panel.setBounds(25, 125, 420, 284);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("Book Name");
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_1.setBounds(10, 49, 100, 24);
panel.add(lblNewLabel_1);
JLabel lblNewLabel_1_1 = new JLabel("Edition");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_1_1.setBounds(10, 102, 100, 24);
panel.add(lblNewLabel_1_1);
JLabel lblNewLabel_1_1_1 = new JLabel("Price");
lblNewLabel_1_1_1.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_1_1_1.setBounds(10, 153, 100, 24);
panel.add(lblNewLabel_1_1_1);
txtbname = new JTextField();
txtbname.setBounds(141, 49, 217, 19);
panel.add(txtbname);
txtbname.setColumns(10);
txtedition = new JTextField();
txtedition.setColumns(10);
txtedition.setBounds(141, 107, 217, 19);
panel.add(txtedition);
txtprice = new JTextField();
txtprice.setColumns(10);
txtprice.setBounds(141, 158, 217, 19);
panel.add(txtprice);
JButton btnNewButton = new JButton("SAVE");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bname,edition,price;
bname=txtbname.getText();
edition=txtedition.getText();
price=txtprice.getText();
try {
pst = con.prepareStatement("insert into Book(name,edition,price)values(?,?,?)");
pst.setString(1, bname);
pst.setString(2,edition);
pst.setString(3,price);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Record Addeddd!!!!");
table_load();
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}
catch (SQLException e1) {
System.out.println("ERROR IN SAVE BTN");
e1.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 12));
btnNewButton.setBounds(38, 462, 99, 48);
frame.getContentPane().add(btnNewButton);
JButton btnExit = new JButton("EXIT ");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setFont(new Font("Tahoma", Font.BOLD, 12));
btnExit.setBounds(181, 462, 99, 48);
frame.getContentPane().add(btnExit);
JButton btnClear = new JButton("CLEAR");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}
});
btnClear.setFont(new Font("Tahoma", Font.BOLD, 12));
btnClear.setBounds(329, 462, 99, 48);
frame.getContentPane().add(btnClear);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(455, 125, 406, 389);
frame.getContentPane().add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "Search", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(38, 532, 404, 69);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel lblNewLabel_1_1_2 = new JLabel("Search");
lblNewLabel_1_1_2.setFont(new Font("Tahoma", Font.BOLD, 15));
lblNewLabel_1_1_2.setBounds(10, 23, 100, 24);
panel_1.add(lblNewLabel_1_1_2);
txtbid = new JTextField();
txtbid.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
try {
String id=txtbid.getText();
pst=con.prepareStatement("Select name,edition,price from Book where id= ?");
pst.setString(1, id);
ResultSet rs=pst.executeQuery();
if(rs.next()==true)
{
String name=rs.getString(1);
String edition =rs.getString(2);
String price=rs.getString(3);
txtbname.setText(name);
txtedition.setText(edition);
txtprice.setText(price);
}
else
{
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
}
}
catch (SQLException ex) {
System.out.println("ERROR IN THE SEARCH");
}
}
});
txtbid.setColumns(10);
txtbid.setBounds(146, 28, 217, 19);
panel_1.add(txtbid);
JButton btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bname,edition,price,bid;
bname=txtbname.getText();
edition=txtedition.getText();
price=txtprice.getText();
bid=txtbid.getText();
try {
pst = con.prepareStatement("update Book set name=?,edition=?,price=? where id=?");
pst.setString(1, bname);
pst.setString(2,edition);
pst.setString(3,price);
pst.setString(4, bid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Record Updated!!!!");
table_load();
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}
catch (SQLException e1) {
System.out.println("ERROR IN SAVE BTN");
e1.printStackTrace();
}
}
});
btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 12));
btnUpdate.setBounds(542, 553, 99, 48);
frame.getContentPane().add(btnUpdate);
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bid;
bid=txtbid.getText();
try {
pst = con.prepareStatement("Delete from Book where id=?");
pst.setString(1, bid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Record Deleted!!!");
table_load();
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}
catch (SQLException e1) {
System.out.println("ERROR IN SAVE BTN");
e1.printStackTrace();
}
}
});
btnDelete.setFont(new Font("Tahoma", Font.BOLD, 12));
btnDelete.setBounds(697, 553, 99, 48);
frame.getContentPane().add(btnDelete);
}
}