-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentDatabaseMySQL.java
244 lines (219 loc) · 7.13 KB
/
StudentDatabaseMySQL.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
import java.sql.*;
import static java.lang.System.out;
import java.util.Scanner;
public class StudentDatabaseMySQL
{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String JDBC_URL = "jdbc:mysql://localhost:3306/";
static final String DB_NAME = "jdbc";
static final String USERNAME = "root";
static final String PASSWORD = "";
public static void main(String[] args)
{
String createQuery, sName;
ResultSet rs;
int id, choice, rows, i = 0, ids[], maxInd;
double mJava, mAlgo, mCrypto, totalM[], max;
Scanner obj = new Scanner(System.in);
boolean check, dropped = false;
try
{
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(JDBC_URL+DB_NAME,USERNAME,PASSWORD);
Statement stmt = conn.createStatement();
/*
// creating table
createQuery = "CREATE TABLE STUDENT ( " +
"STUDENT_ID INTEGER PRIMARY KEY,"+
"STUDENT_NAME VARCHAR(30),"+
"MARKS_JAVA DOUBLE,"+
"MARKS_ALGO DOUBLE,"+
"MARKS_CRYPTO DOUBLE )";
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("STUDENT table successfully created...");
*/
do
{
out.println("\n----MENU----\n1.Display all Records"
+ "\n2. Add a record\n"
+ "3. Delete a record\n"
+ "4. Display records of best student\n"
+ "5. Scale up marks in Algorithm\n"
+ "6. Delete all rows\n"
+ "7. DROP table\n"
+ "8. Exit\n"
+ "Enter choice: ");
choice = obj.nextInt();
switch(choice)
{
case 1: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
createQuery = "SELECT * FROM STUDENT";
rs = stmt.executeQuery(createQuery);
check = false;
if(rs.next())
check = true;
if(check)
{
createQuery = "SELECT * FROM STUDENT";
out.println("\n"+createQuery+"\n");
rs = stmt.executeQuery(createQuery);
System.out.println("\n\nID \t\t NAMES \t\t\t\t\t JAVA \t\t ALGO \t\t CRYPTO \n");
while(rs.next())
System.out.print(rs.getInt(1)+"\t\t"+rs.getString(2)+"\t\t\t\t"+rs.getDouble(3)+"\t\t"+rs.getDouble(4)+"\t\t"+rs.getDouble(5)+"\n");
}
else
out.println("\nNothing to display...\n");
break;
case 2: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
out.println("\nEnter ID: ");
id = obj.nextInt();
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+id;
rs = stmt.executeQuery(createQuery);
check = false;
if(rs.next())
check = true;
if(check)
out.println("\nID already exists...\n");
else
{
out.println("\nEnter Name: ");
obj.nextLine();
sName = obj.nextLine();
out.println("\nEnter marks in JAVA: ");
mJava = obj.nextDouble();
out.println("\nEnter marks in ALGORITHM: ");
mAlgo = obj.nextDouble();
out.println("\nEnter marks in CRYPTOGRAPHY: ");
mCrypto = obj.nextDouble();
createQuery = "INSERT INTO STUDENT VALUES ( "+id+" , '"+sName+"' , "+mJava+" , "+mAlgo+" , "+mCrypto+" )";
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("\nRecord successfully inserted...\n");
}
break;
case 3: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
out.println("\nEnter ID: ");
id = obj.nextInt();
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+id;
rs = stmt.executeQuery(createQuery);
check = false;
if(rs.next())
check = true;
if(!check)
out.println("\nID does not exists...\n");
else
{
createQuery = "DELETE FROM STUDENT WHERE STUDENT_ID = "+id;
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("\nRecord successfully deleted...\n");
}
break;
case 4: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
createQuery = "SELECT * FROM STUDENT";
rs = stmt.executeQuery(createQuery);
check = false;
if(rs.next())
check = true;
if(!check)
out.println("\nNo data retrieved...\n");
else
{
rows = 0;
createQuery = "SELECT * FROM STUDENT";
rs = stmt.executeQuery(createQuery);
while(rs.next())
++rows;
totalM = new double[rows];
ids = new int[rows];
createQuery = "SELECT * FROM STUDENT";
rs = stmt.executeQuery(createQuery);
while(rs.next())
{
totalM[i] = rs.getDouble(3) + rs.getDouble(4) + rs.getDouble(5);
ids[i] = rs.getInt(1);
++i;
}
i = 0;
max = totalM[0];
maxInd = ids[0];
for( i = 0; i < totalM.length; ++i )
if( totalM[i] > max )
{
max = totalM[i];
maxInd = ids[i];
}
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+maxInd;
out.println(createQuery);
rs = stmt.executeQuery(createQuery);
System.out.println("\n\nID \t\t NAME \n");
while(rs.next())
System.out.print(rs.getInt(1)+"\t\t"+rs.getString(2)+"\n");
}
break;
case 5: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
createQuery = "UPDATE STUDENT SET MARKS_ALGO = MARKS_ALGO + 5 WHERE MARKS_ALGO < 96";
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("\nUpdated successfully...\n");
break;
case 6: if(dropped)
{
out.println("\nSTUDENT table does not exist in database...\n");
break;
}
createQuery = "DELETE FROM STUDENT";
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("\nSTUDENT table emptied...\n");
break;
case 7: if(!dropped)
{
dropped = true;
createQuery = "DROP TABLE STUDENT";
out.println(createQuery);
stmt.executeUpdate(createQuery);
out.println("STUDENT table successfully dropped...");
}
else
out.println("\nSTUDENT table does not exist in database...\n");
break;
case 8: out.println("\nPLEASURE TO SERVE YOU!!\n");
break;
default: out.println("\nINVALID CHOICE...\n");
}
}
while(choice != 8);
}
catch(ClassNotFoundException cnfe)
{
out.println("Error loading MySQL Driver!");
}
catch(SQLException se)
{
se.printStackTrace();
out.println("Error accessing Database!");
}
}
}