-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionOperations.java
361 lines (319 loc) · 12.8 KB
/
ConnectionOperations.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.student.attedence;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
public class ConnectionOpratios {
public static boolean InsertStudentToDB(Attendence st) {
boolean success=false;
try {
//JDBC CODE............
Connection con=ConnectionEstablish.createRelation();
//QUERY.....
String Query="insert into students_attendence(ROLL,stream,name,year,enroll,classid,date,sec) values(?,?,?,?,?,?,?,?)";
//PREPARED STATEMENT....
PreparedStatement pst=con.prepareStatement(Query);
//SET VALUES 1 BY 1
pst.setString(1, st.getRoll());
pst.setString(2, st.getStream());
pst.setString(3, st.getStnm());
pst.setString(4, st.getYr());
pst.setString(5, st.getEnroll());
pst.setString(6, st.getClassId());
pst.setString(7, st.getDate());
pst.setString(8, st.getSec());
//EXECUTE
pst.executeUpdate();
autoIdUpdate();
success=true;
}
catch(Exception e){
e.printStackTrace();
}
return success;
}
public static void viewAll() {
try {
Connection con=ConnectionEstablish.createRelation();
Statement smt=con.createStatement();
String Query="select * from students_attendence";
ResultSet rs=smt.executeQuery(Query);
System.out.println("*************************************************************************************************************************************************************************************************************");
System.out.printf("%-10s%-20s%-25s%-35s%-25s%-20s%-15s%-15s%-20s\n","ID","STUDENT ROLLS","STREAM","STUDENT NAME","YEAR","ENROLLMENT NO","CLASS ID","SECTION","DATE & TIME");
System.out.println("*************************************************************************************************************************************************************************************************************");
if(rs.next()){
do{
System.out.printf("%-10s%-20s%-25s%-35s%-25s%-20s%-15s%-15s%-20s",rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(9),rs.getString(8));
System.out.println();
}while(rs.next());
}
else{
System.out.println("Record Not Found...");
}
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void fetchAttendence(String classid) {
try {
Connection con=ConnectionEstablish.createRelation();
String Q=String.format("select name,ROLL,sec,stream,year,date from students_attendence where classid=\"%s\" order by sec asc",classid);
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
System.out.println("*************************************************************************************************************************************************************************************************************");
System.out.printf("%-50s%-10s%-15s%-15s%-15s%-25s\n","NAME","ROLL","SECTION","STREAM","YEAR","DATE & TIME");
System.out.println("*************************************************************************************************************************************************************************************************************");
if(rs.next()){
do{
System.out.printf("%-50s%-10s%-15s%-15s%-15s%-25s",rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
System.out.println();
}while(rs.next());
}
else{
System.out.println("Record Not Found...");
}
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static String[] findStudentForAttendence(String enroll) {
String classids[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
Statement smt=con.createStatement();
Statement smt2=con.createStatement();
String q1=String.format("select name,year,stream,ROLL,classid,date from students_attendence where enroll=\"%s\" order by classid asc",enroll);
ResultSet rs=smt.executeQuery(q1);
System.out.println("*************************************************************************************************************************************************************************************************************");
System.out.printf("%-30s%-15s%-15s%-15s%-15s%-25s\n","NAME","YEAR","STREAM","ROLL","CLASS_ID","DATE & TIME");
System.out.println("*************************************************************************************************************************************************************************************************************");
int i=0;
if(rs.next()){
do{
System.out.printf("%-30s%-15s%-15s%-15s%-15s%-25s",rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
System.out.println();
classids=Arrays.copyOf(classids,classids.length+1);
classids[i]=rs.getString(5);
i++;
}while(rs.next());
con.close();
return classids;
}
else{
System.out.println("Record Not Found...");
return classids;
}
}
catch(Exception e) {
e.printStackTrace();
return classids;
}
}
public static void delete(int id) {
try {
Connection con=ConnectionEstablish.createRelation();
String Query1=String.format("select name from students_attendence where sno=%d",id);
Statement smt=con.createStatement();
ResultSet rs = smt.executeQuery(Query1);
if(rs.next()) {
do {
System.out.println("STUDENT NAME :: "+rs.getString(1));
System.out.println("DELETED SUCCESSFULLY !!!!");
}while(rs.next());}
else {
System.out.println("Record Not Found...");
}
String Query="delete from students_attendence where sno=?";
PreparedStatement pstmt=con.prepareStatement(Query);
pstmt.setInt(1,id);
pstmt.executeUpdate();
autoIdUpdate();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static String[] disclassesArray() {
String classes[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select distinct classid from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
classes=Arrays.copyOf(classes,classes.length+1);
classes[i]=rs.getString(1);
i++;
}while(rs.next());
return classes;
}
return classes;
}catch(Exception e)
{
e.printStackTrace();
return classes;
}
}
public static void autoIdUpdate() {
try {
String Q1="SET @num := 0";
String Q2="UPDATE students_attendence SET SNO = @num := (@num+1)";
String Q3="ALTER TABLE students_attendence AUTO_INCREMENT =1";
Connection con=ConnectionEstablish.createRelation();
PreparedStatement pst1=con.prepareStatement(Q1);
PreparedStatement pst2=con.prepareStatement(Q2);
PreparedStatement pst3=con.prepareStatement(Q3);
pst1.executeUpdate();
pst2.executeUpdate();
pst3.executeUpdate();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static String[] enrollsArray() {
String stens[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select enroll from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
stens=Arrays.copyOf(stens,stens.length+1);
stens[i]=rs.getString(1);
i++;
}while(rs.next());
return stens;
}
return stens;
}catch(Exception e)
{
e.printStackTrace();
return stens;
}
}
public static String[] secArray() {
String stsec[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select sec from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
stsec=Arrays.copyOf(stsec,stsec.length+1);
stsec[i]=rs.getString(1);
i++;
}while(rs.next());
return stsec;
}
return stsec;
}catch(Exception e)
{
e.printStackTrace();
return stsec;
}
}
public static String[] streamArray() {
String streams[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select stream from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
streams=Arrays.copyOf(streams,streams.length+1);
streams[i]=rs.getString(1);
i++;
}while(rs.next());
return streams;
}
return streams;
}catch(Exception e)
{
e.printStackTrace();
return streams;
}
}
public static String[] rollsArray() {
String rolls[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select ROLL from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
rolls=Arrays.copyOf(rolls,rolls.length+1);
rolls[i]=rs.getString(1);
i++;
}while(rs.next());
return rolls;
}
return rolls;
}catch(Exception e)
{
e.printStackTrace();
return rolls;
}
}
public static String[] classesArray() {
String classes[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select classid from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
classes=Arrays.copyOf(classes,classes.length+1);
classes[i]=rs.getString(1);
i++;
}while(rs.next());
return classes;
}
return classes;
}catch(Exception e)
{
e.printStackTrace();
return classes;
}
}
public static String[] datesArray() {
String dates[] = new String[0];
try {
Connection con=ConnectionEstablish.createRelation();
String Q="select date from students_attendence";
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery(Q);
int i=0;
if(rs.next()) {
do {
dates=Arrays.copyOf(dates,dates.length+1);
dates[i]=rs.getString(1);
i++;
}while(rs.next());
return dates;
}
return dates;
}catch(Exception e)
{
e.printStackTrace();
return dates;
}
}
}