-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.java
325 lines (320 loc) · 7.82 KB
/
gui.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
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.io.*;
public class gui extends JFrame
{
private JLabel l1,id,vow,con,course,caps,pun,l2,l3,l4;
private JTextField in,gvow,gcon,gpun,dob,crse;
private JButton cmd,sd;
private JComboBox cb;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private PreparedStatement ps;
public gui()
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Sdbms?useSSL=false","root","root");
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println(e);
}
this.setSize(500,700);
l1=new JLabel("Student DBMS");
l1.setHorizontalAlignment(SwingConstants.CENTER);
l1.setFont(new Font("Serif", Font.ROMAN_BASELINE, 30));
l1.setBounds(25, 10, 450, 50);
in = new JTextField();
in.setBounds(175, 225, 150, 20);
this.add(in);
id = new JLabel("Enter Student ID");
id.setBounds(125, 190, 250, 25);
id.setHorizontalAlignment(SwingConstants.CENTER);
gvow = new JTextField();
gvow.setBounds(250, 300, 200, 20);
this.add(gvow);
l2=new JLabel();
l2.setBounds(175, 260, 250, 25);
l2.setForeground(Color.red);
in.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
KeyPressed(ke,l2);
}
});
cmd=new JButton("Add");
cmd.setBounds(350, 550, 100, 25);
cmd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
cmdActionPerformed();
}
});
sd=new JButton("Show Details");
sd.setBounds(300, 550, 150, 25);
sd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
sdActionPerformed();
}
});
sd.setVisible(false);
String st[]= {"Check","Add","Update","Delete"};
cb=new JComboBox(st);
cb.setBounds(200, 125, 100, 20);
cb.setSelectedIndex(1);
cb.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
cbItemStateChanged();
}
});
gcon = new JTextField();
gcon.setBounds(250, 350, 200, 20);
this.add(gcon);
l3=new JLabel();
l3.setBounds(250, 420, 200, 20);
l3.setForeground(Color.red);
this.add(l3);
gpun = new JTextField();
gpun.setBounds(250, 400, 200, 20);
gpun.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
KeyPressed(ke,l3);
}
});
this.add(gpun);
dob = new JTextField();
dob.setBounds(250, 450, 200, 20);
this.add(dob);
crse = new JTextField();
crse.setBounds(250, 500, 200, 20);
this.add(crse);
vow = new JLabel("First Name");
con = new JLabel("Last Name");
pun = new JLabel("Grade");
caps = new JLabel("DOB");
course = new JLabel("Course");
vow.setBounds(75, 300, 200, 20);
vow.setHorizontalAlignment(SwingConstants.LEFT);
course.setBounds(75, 500, 200, 20);
course.setHorizontalAlignment(SwingConstants.LEFT);
con.setBounds(75, 350, 200, 20);
con.setHorizontalAlignment(SwingConstants.LEFT);
pun.setBounds(75, 400, 200, 20);
pun.setHorizontalAlignment(SwingConstants.LEFT);
caps.setBounds(75, 450, 200, 20);
caps.setHorizontalAlignment(SwingConstants.LEFT);
l4=new JLabel("");
l4.setBounds(220, 600, 200, 20);
l4.setForeground(Color.red);
this.add(l1);
this.add(l2);
this.add(l4);
this.add(vow);
this.add(con);
this.add(pun);
this.add(caps);
this.add(course);
this.add(id);
this.add(cb);
this.add(cmd);
this.add(sd);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent e) {
l2.setText("");
l3.setText("");
in.setEditable(true);
if(cb.getSelectedIndex()==1 || cb.getSelectedIndex()==2)
{
gpun.setEditable(true);
}
}
public void mouseDragged(MouseEvent e)
{}
});
this.setLayout(null);
this.setVisible(true);
}
private void cbItemStateChanged()
{
String s=cb.getSelectedItem().toString();
int m=cb.getSelectedIndex();
cmd.setText(s);
cmd.setVisible(true);
in.setText("");
gvow.setText("");
gcon.setText("");
gpun.setText("");
dob.setText("");
crse.setText("");
l4.setText("");
if(m==0 || m==3)
{
gvow.setEditable(false);
gcon.setEditable(false);
gpun.setEditable(false);
dob.setEditable(false);
crse.setEditable(false);
if(m==3)
{
sd.setVisible(true);
cmd.setVisible(false);
}
}
else
{
gvow.setEditable(true);
gcon.setEditable(true);
gpun.setEditable(true);
dob.setEditable(true);
crse.setEditable(true);
}
}
private void cmdActionPerformed()
{
try
{
l4.setText("");
if(!in.getText().equals(""))
{
int idEntered =Integer.parseInt(in.getText());
if(cb.getSelectedIndex()==1)
{
rs=stmt.executeQuery("SELECT * FROM info WHERE ID = "+idEntered);
if(!rs.next())
{
ps=conn.prepareStatement("INSERT INTO info (ID,fname,lname,grade,dob,course) VALUES (?,?,?,?,?,?)");
ps.setInt(1, idEntered);
ps.setString(2,gvow.getText());
ps.setString(3, gcon.getText());
ps.setInt(4, Integer.parseInt(gpun.getText()));
ps.setDate(5, Date.valueOf(dob.getText()));
ps.setString(6, crse.getText());
ps.executeUpdate();
}
else
{
l4.setText("* Entery Already Exists!!!!");
}
}
else
{
rs=stmt.executeQuery("SELECT * FROM info WHERE ID = "+idEntered);
if(rs.next())
{
if (cb.getSelectedIndex()==0)
{
gvow.setText(rs.getString(2));
gcon.setText(rs.getString(3));
gpun.setText(rs.getString(4));
dob.setText(rs.getDate(5).toString());
crse.setText(rs.getString(6));
}
else if(cb.getSelectedIndex()==3)
{
stmt.executeUpdate("DELETE FROM info WHERE ID = "+idEntered);
}
else if(cb.getSelectedIndex()==2)
{
String A="";
int i=0;
if(!gvow.getText().equals(""))
{
A=A+"fname= \""+gvow.getText()+"\", ";
i++;
gvow.setText((""));
}
if(!gcon.getText().equals(""))
{
A=A+"lname= \""+gcon.getText()+"\", ";
i++;
gcon.setText((""));
}
if(!gpun.getText().equals(""))
{
A=A+"grade= "+gpun.getText()+",";
i++;
gpun.setText((""));
}
if(!dob.getText().equals(""))
{
A=A+"dob= \""+dob.getText()+"\",";
i++;
dob.setText("");
}
if(!crse.getText().equals(""))
{
A=A+"course= \"" +crse.getText()+"\",";
i++;
crse.setText((""));
}
if(i>0)
{
A=A.substring(0,A.length()-1);
stmt.executeUpdate("UPDATE info SET "+A+" WHERE ID= "+idEntered);
}
else
l4.setText("* No Field Updated!!!");
}
}
else
{
l4.setText("* Entery doesn't exists!!!!!");
}
}
}
else
{
l4.setText(("* No ID entered!!!"));
}
if(cb.getSelectedIndex()!=0)
{
in.setText("");
gvow.setText("");
gcon.setText("");
gpun.setText("");
dob.setText("");
crse.setText("");
}
}
catch (Exception e) {
System.out.println(e);
}
}
private void sdActionPerformed()
{
try{
rs=stmt.executeQuery("SELECT * FROM info WHERE ID = "+Integer.parseInt(in.getText()));
if(rs.next())
{
gvow.setText(rs.getString(2));
gcon.setText(rs.getString(3));
gpun.setText(rs.getString(4));
dob.setText(rs.getDate(5).toString());
crse.setText(rs.getString(6));
}
sd.setVisible(false);
cmd.setVisible(true);
}
catch (Exception e) {
System.out.println(e);
}
}
private void KeyPressed(KeyEvent ke,JLabel l) {
JTextField j=(JTextField)ke.getSource();
if ((ke.getKeyChar() >= '0' && ke.getKeyChar() <= '9')||ke.getKeyCode() == KeyEvent.VK_BACK_SPACE||ke.getKeyCode() == KeyEvent.VK_DELETE) {
j.setEditable(true);
l.setText("");
} else {
j.setEditable(false);
l.setText("* Enter only numeric digits(0-9)");
}
}
public static void main(String[] args) {
gui g=new gui();
}
}