-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUDOperation.java
More file actions
333 lines (266 loc) · 8.3 KB
/
Copy pathCRUDOperation.java
File metadata and controls
333 lines (266 loc) · 8.3 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
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;
//Make one table as student, with
//roll number, student 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 CRUDOperation
{
public static void main(String[] args)
{
Student s = new Student();
int ch;
do
{
System.out.print("1.Inser \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 Student
{
void insertDetails()
{
try {
System.out.println("*****Insert student 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 student roll no: ");
rollno = sc.nextInt();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student where rollno="+rollno);
if(rs.next())
{
throw new StudentAlreadyExistsException("student already exists.....");
}
System.out.println("enter student name: ");
name = sc.next();
System.out.println("Enter birth date: ");
birthdate =sc.next() ;
//Date date1 = Date.valueOf(sc.next());
System.out.println("enter student physics marks: ");
physics = sc.nextInt();
System.out.println("enter student chemistry marks: ");
chemistry = sc.nextInt();
System.out.println("enter student 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 student values(?,?,?,?,?,?,?,?)");
pst.setInt(1, rollno);
pst.setString(2, name);
pst.setString(3, birthdate);
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 (StudentAlreadyExistsException e) {
e.printStackTrace();
}
}
void displayDetails()
{
try {
System.out.println("*****Display student 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 student");
while(rs.next())
{
System.out.println("Student roll no "+rs.getInt(1));
System.out.println("Student name: "+rs.getString(2));
System.out.println("Student birthdate: "+rs.getString(3));
System.out.println("Student physics: "+rs.getInt(4));
System.out.println("Student chemistry: "+rs.getInt(5));
System.out.println("Student maths: "+rs.getInt(6));
System.out.println("Student total: "+rs.getInt(7));
System.out.println("Student grade: "+rs.getString(8));
System.out.println("----------------------");
}
rs.close();
statement.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
void updateDetails()
{
System.out.println("****Update student 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 student set sname=?, birthdate=?, physics=?, chemistry=?, maths=?, total=?, grade=? where rollno=?");
System.out.println("Prepared statement created");
System.out.println("Enter student rollno: ");
Scanner sc = new Scanner(System.in);
rollno = sc.nextInt();
String ch;
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student where rollno="+rollno);
if(rs.next())
{
System.out.println("Do you want to update student name ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter student name: ");
String sname = sc.next();
pst.setString(1, sname);
}
else
{
pst.setString(1, rs.getString("sname"));
}
System.out.println("Do you want to update student birthdate ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter student birthdate: ");
String birthdate = sc.next();
pst.setString(2, birthdate);
}
else
{
pst.setString(2, rs.getString("birthdate"));
}
System.out.println("Do you want to update student physics marks ?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter student physics marks: ");
physics = sc.nextInt();
pst.setInt(3, physics);
}
else
{
pst.setString(3, rs.getString("physics"));
}
System.out.println("Do you want to update student chemistry marks?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter student chemistry marks: ");
chemistry = sc.nextInt();
pst.setInt(4, chemistry);
}
else
{
pst.setString(4, rs.getString("chemistry"));
}
System.out.println("Do you want to update student maths marks?");
ch = sc.next();
if(ch.equals("y"))
{
System.out.println("Enter student maths marks: ");
maths = sc.nextInt();
pst.setInt(5, maths);
}
else
{
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 StudentNotFoundException("Student not found with rollno "+rollno);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (StudentNotFoundException e) {
e.printStackTrace();
}
}
void deleteDetails()
{
try {
System.out.println("*****Delete student 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 student where rollno="+rollno);
if(rs.next())
{
PreparedStatement pst = con.prepareStatement("delete from student 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 StudentNotFoundException("Student not found with rollno: "+rollno);
}
}
catch (SQLException e) {
e.printStackTrace();
} catch (StudentNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}