-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBooks.java
257 lines (218 loc) · 8.1 KB
/
AddBooks.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
package library1;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class AddBooks extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField t1, t2, t3, t4, t5, t6;
private JButton b1, b2;
private static int left=20;//buttonleft
private static int textleft = 137;
private static int buttonWidth=88, buttonHeight=33;
JComboBox<Object> comboBox;
public static void main(String[] args) {
new AddBooks().setVisible(true);
}
public void random() {
Random rd = new Random();
t1.setText("" + rd.nextInt(100000 + 1));
}
public AddBooks() {
super("Add Books");
setBounds(500, 200, 400, 442);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel l6 = new JLabel("Book_id");
l6.setForeground(new Color(47, 79, 79));
l6.setFont(new Font("Tahoma", Font.BOLD, 14));
l6.setBounds(left, 51, 90, 22);
contentPane.add(l6);
JLabel l1 = new JLabel("Name");
l1.setForeground(new Color(47, 79, 79));
l1.setFont(new Font("Tahoma", Font.BOLD, 14));
l1.setBounds(left, 84, 90, 22);
contentPane.add(l1);
JLabel l2 = new JLabel("ISBN");
l2.setForeground(new Color(47, 79, 79));
l2.setFont(new Font("Tahoma", Font.BOLD, 14));
l2.setBounds(left, 117, 90, 22);
contentPane.add(l2);
JLabel l3 = new JLabel("Publisher");
l3.setForeground(new Color(47, 79, 79));
l3.setFont(new Font("Tahoma", Font.BOLD, 14));
l3.setBounds(left, 150, 90, 22);
contentPane.add(l3);
JLabel l7 = new JLabel("Edition");
l7.setForeground(new Color(47, 79, 79));
l7.setFont(new Font("Tahoma", Font.BOLD, 14));
l7.setBounds(left, 183, 90, 22);
contentPane.add(l7);
JLabel l4 = new JLabel("Price");
l4.setForeground(new Color(47, 79, 79));
l4.setFont(new Font("Tahoma", Font.BOLD, 14));
l4.setBounds(left, 216, 90, 22);
contentPane.add(l4);
JLabel l5 = new JLabel("Pages");
l5.setForeground(new Color(47, 79, 79));
l5.setFont(new Font("Tahoma", Font.BOLD, 14));
l5.setBounds(left, 249, 90, 22);
contentPane.add(l5);
//------------------------------textField ---------------------------------------------------------------------------------------------------------------------
// bookId ko
t1 = new JTextField();
t1.setForeground(new Color(47, 79, 79));
t1.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t1.setBounds(textleft, 54, 198, 20);
t1.setColumns(10);
contentPane.add(t1);
t2 = new JTextField();// name ko
t2.setForeground(new Color(47, 79, 79));
t2.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t2.setColumns(10);
t2.setBounds(textleft, 87, 198, 20);
contentPane.add(t2);
t3 = new JTextField();// isb ko
t3.setForeground(new Color(47, 79, 79));
t3.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t3.setColumns(10);
t3.setBounds(textleft, 120, 198, 20);
contentPane.add(t3);
t4 = new JTextField();// publisher ko
t4.setForeground(new Color(47, 79, 79));
t4.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t4.setColumns(10);
t4.setBounds(textleft, 153, 198, 20);
contentPane.add(t4);
comboBox = new JComboBox<Object>();
String[] values = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
DefaultComboBoxModel<Object> defaultValues = new DefaultComboBoxModel<Object>(values);
comboBox.setModel(defaultValues);
comboBox.setBounds(textleft, 186, 194, 20);
contentPane.add(comboBox);
t5 = new JTextField();// price
t5.setForeground(new Color(47, 79, 79));
t5.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t5.setColumns(10);
t5.setBounds(textleft, 219, 198, 20);
contentPane.add(t5);
t6 = new JTextField();// page
t6.setForeground(new Color(47, 79, 79));
t6.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
t6.setColumns(10);
t6.setBounds(textleft, 252, 198, 20);
contentPane.add(t6);
//-------------------button----------------------------------------------------------------------------------
b1 = new JButton("Add");
b1.addActionListener(this);
b1.setBorder(new CompoundBorder(new LineBorder(new Color(128, 128, 128)), null));
b1.setFont(new Font("Trebuchet MS", Font.BOLD, 15));
b1.setBounds(textleft, 300, buttonWidth, buttonHeight);
b1.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
contentPane.add(b1);
b2 = new JButton("Back");
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
try {
if (ae.getSource() == b2) {
setVisible(false);
new home().setVisible(true);
}
} catch (Exception e2) {
// TODO: handle exception
}
}
});
b2.setBorder(new CompoundBorder(new LineBorder(new Color(105, 105, 105)), null));
b2.setFont(new Font("Trebuchet MS", Font.BOLD, 15));
b2.setBounds(250, 300, buttonWidth, buttonHeight);
b2.setBackground(Color.BLACK);
b2.setForeground(Color.WHITE);
contentPane.add(b2);
//-----------------------------------------------------------------------------------------------------
//
// JPanel panel = new JPanel();
// panel.setBorder(new TitledBorder(new LineBorder(new Color(138, 43, 226), 2), "Add Books", TitledBorder.LEADING,
// TitledBorder.TOP, null, new Color(0, 0, 255)));
// panel.setBounds(10, 29, 398, 344);
// contentPane.add(panel);
//
// panel.setBackground(Color.WHITE);
// contentPane.setBackground(Color.WHITE);
random();
}
public void actionPerformed(ActionEvent ae) {
try {
conn con = new conn();
String bookId = t1.getText();
String name = t2.getText();
String Isbko = t3.getText();
String PublisherName = t4.getText();
String price = t5.getText();
String pages = t6.getText();
String comboValue = (String) comboBox.getSelectedItem();
// how to text price is int or not
// int test = Integer.parseInt(price);
if (ae.getSource() == b2) {
this.setVisible(false);
new home().setVisible(true);
}
if ( name.isEmpty() && Isbko.isEmpty() && PublisherName.isEmpty() && price.isEmpty() && pages.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Someting!!!!");
} else if (name.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Your name!!!!");
} else if (Isbko.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Your Isbook!!!!");
} else if (PublisherName.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Publisher Name!!!!");
} else if (price.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Price of Book !!!!");
} else if (pages.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please insert Book Pages!!!!");
}
else {
if (ae.getSource() == b1) {
String sql = "insert into book(book_id, name, isbn, publisher, edition, price, pages) values( '"
+ bookId + "', '" + name + "', '" + Isbko + "', '" + PublisherName + "','" + comboValue
+ "', '" + price + "', '" + pages + "')";
int rs = con.s.executeUpdate(sql);
// PreparedStatement st = con.c.prepareStatement(sql);
// // st.setInt(1, Integer.parseInt(textField.getText()));
// st.setString(1, t1.getText());
// st.setString(2, t2.getText());
// st.setString(3, t3.getText());
// st.setString(4, t4.getText());
// st.setString(5, (String) comboBox.getSelectedItem());
// st.setString(6, t5.getText());
// st.setString(7, t6.getText());
//
// int rs = st.executeUpdate();
if (rs > 0)
JOptionPane.showMessageDialog(null, "Successfully Added");
else
JOptionPane.showMessageDialog(null, "Error");
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
// st.close();
}
con.c.close();
}
} catch (Exception e) {
}
}
}