-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUD.java
More file actions
339 lines (272 loc) · 8.56 KB
/
Copy pathCRUD.java
File metadata and controls
339 lines (272 loc) · 8.56 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
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
// rollno dall
//Make one table as Student1, with
//roll number, Student1 name, birthdate, physics, chemistry, maths, total, grade
//
//and perform INSERT, UPDATE, DELETE, SELECT AND SELECT ALL RECORDS OPERATIONS OVER IT USING THE JDBC
public class CRUD
{
public static void main(String[] args)
{
Student1 s = new Student1();
int ch;
do
{
System.out.print("1.Insert \n2.Update \n3.Delete \n4.SelectAll\n5.Exit");
System.out.println("Enter the choice");
Scanner sc = new Scanner(System.in);
ch = sc.nextInt();
switch(ch)
{
case 1: s.insertDetails();
break;
case 2: s.updateDetails();
break;
case 3: s.deleteDetails();
break;
case 4: s.displayDetails();
break;
case 5: System.exit(0);
}
}while(ch!=5);
}
}
class Student1
{
void insertDetails()
{
try {
System.out.println("*****Insert Student1 details****");
int rollno, physics, chemistry, maths, total;
String name, grade, birthdate;
DriverManager.registerDriver(new org.hsqldb.jdbc.JDBCDriver());
Connection con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb");
Scanner sc = new Scanner(System.in);
System.out.println("enter Student1 roll no: ");
rollno = sc.nextInt();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Studentdetails where rollno="+rollno);
if(rs.next())
{
throw new Student1AlreadyExistsException("Student1 already exists.....");
}
System.out.println("enter Student1 name: ");
name = sc.next();
System.out.println("Enter birth date: ");
String dob = sc.next();
Date date = Date.valueOf(dob);
//birthdate =sc.next() ;
//Date date1 = Date.valueOf(sc.next());
System.out.println("enter Student1 physics marks: ");
physics = sc.nextInt();
System.out.println("enter Student1 chemistry marks: ");
chemistry = sc.nextInt();
System.out.println("enter Student1 maths marks: ");
maths = sc.nextInt();
total = (physics + chemistry + maths);
if(total >= 200)
grade="A";
else if(total >= 100)
grade = "B";
else
grade = "C";
PreparedStatement pst = con.prepareStatement("insert into Studentdetails values(?,?,?,?,?,?,?,?)");
pst.setInt(1, rollno);
pst.setString(2, name);
pst.setDate(3, date);
pst.setInt(4, physics);
pst.setInt(5, chemistry);
pst.setInt(6, maths);
pst.setInt(7, total);
pst.setString(8, grade);
int rows = pst.executeUpdate();
System.out.println("Rows affected..."+rows);
pst.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Student1AlreadyExistsException e) {
e.printStackTrace();
}
}
void displayDetails()
{
try {
System.out.println("*****Display Student1 details*****");
DriverManager.registerDriver(new org.hsqldb.jdbc.JDBCDriver());
System.out.println("driver registered");
Connection con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
Statement statement = con.createStatement();
System.out.println("statement created");
ResultSet rs = statement.executeQuery("select * from Studentdetails");
while(rs.next())
{
System.out.println("Student1 roll no "+rs.getInt(1));
System.out.println("Student1 name: "+rs.getString(2));
System.out.println("Student1 birthdate: "+rs.getString(3));
System.out.println("Student1 physics: "+rs.getInt(4));
System.out.println("Student1 chemistry: "+rs.getInt(5));
System.out.println("Student1 maths: "+rs.getInt(6));
System.out.println("Student1 total: "+rs.getInt(7));
System.out.println("Student1 grade: "+rs.getString(8));
System.out.println("----------------------");
}
rs.close();
statement.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
void updateDetails()
{
System.out.println("****Update Student1 details****");
try
{
int rollno, physics=0, chemistry=0, maths=0,total=0;
String name, grade;
DriverManager.registerDriver(new org.hsqldb.jdbc.JDBCDriver());
Connection con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
PreparedStatement pst = con.prepareStatement("update Studentdetails set sname=?, birthdate=?, physics=?, chemistry=?, maths=?, total=?, grade=? where rollno=?");
System.out.println("Prepared statement created");
System.out.println("Enter Student1 rollno: ");
Scanner sc = new Scanner(System.in);
rollno = sc.nextInt();
String ch;
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Studentdetails where rollno="+rollno);
if(rs.next())
{
System.out.println("Do you want to update Student1 name ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter Student1 name: ");
String sname = sc.next();
pst.setString(1, sname);
}
else
{
pst.setString(1, rs.getString("sname"));
}
System.out.println("Do you want to update Student1 birthdate ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter Student1 birthdate: ");
String birthdate = sc.next();
pst.setString(2, birthdate);
}
else
{
pst.setString(2, rs.getString("birthdate"));
}
System.out.println("Do you want to update Student1 physics marks ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter Student1 physics marks: ");
physics = sc.nextInt();
pst.setInt(3, physics);
}
else
{
physics = rs.getInt("physics");
pst.setString(3, rs.getString("physics"));
}
System.out.println("Do you want to update Student1 chemistry marks?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter Student1 chemistry marks: ");
chemistry = sc.nextInt();
pst.setInt(4, chemistry);
}
else
{
chemistry = rs.getInt("chemistry");
pst.setString(4, rs.getString("chemistry"));
}
System.out.println("Do you want to update Student1 maths marks?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter Student1 maths marks: ");
maths = sc.nextInt();
pst.setInt(5, maths);
}
else
{
maths = rs.getInt("maths");
pst.setString(5, rs.getString("maths"));
}
total = physics+chemistry+maths;
if(total >= 200)
grade="A";
else if(total >= 100)
grade = "B";
else
grade = "C";
pst.setInt(6, total);
pst.setString(7, grade);
pst.setInt(8, rollno);
int row = pst.executeUpdate();
rs.close();
pst.close();
con.close();
System.out.println("Rows :"+row);
}
else
{
throw new Student1NotFoundException("Student1 not found with rollno "+rollno);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Student1NotFoundException e) {
e.printStackTrace();
}
}
void deleteDetails()
{
try {
System.out.println("*****Delete Student1 details****");
DriverManager.registerDriver(new org.hsqldb.jdbc.JDBCDriver());
Connection con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb");
Scanner sc = new Scanner(System.in);
int rollno = sc.nextInt();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Studentdetails where rollno="+rollno);
if(rs.next())
{
PreparedStatement pst = con.prepareStatement("delete from Studentdetails where rollno = ?");
pst.setInt(1, rollno);
int rows = pst.executeUpdate();
System.out.println("Rows updated : "+rows);
rs.close();
pst.close();
st.close();
con.close();
}
else
{
throw new Student1NotFoundException("Student1 not found with rollno: "+rollno);
}
}
catch (SQLException e) {
e.printStackTrace();
} catch (Student1NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}