Skip to content

Commit a259175

Browse files
authored
JDBC
1 parent ffe097b commit a259175

File tree

2 files changed

+295
-0
lines changed

2 files changed

+295
-0
lines changed

PreparedStatements.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.sql.*;
2+
import static java.lang.System.out;
3+
4+
public class PreparedStatements
5+
{
6+
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
7+
static final String JDBC_URL = "jdbc:mysql://localhost:3306/";
8+
static final String DB_NAME = "jdbc";
9+
static final String USERNAME = "root";
10+
static final String PASSWORD = "";
11+
12+
public static void main(String[] args)
13+
{
14+
String createQuery;
15+
PreparedStatement stmt = null;
16+
ResultSet rs = null;
17+
18+
try
19+
{
20+
Class.forName(JDBC_DRIVER);
21+
22+
Connection conn = DriverManager.getConnection(JDBC_URL + DB_NAME, USERNAME, PASSWORD);
23+
24+
createQuery = "SELECT * FROM STUDENT";
25+
out.println(createQuery);
26+
stmt = conn.prepareStatement(createQuery);
27+
rs = stmt.executeQuery();
28+
System.out.println("\n\nID \t\t NAMES \t\t\t\t\t JAVA \t\t ALGO \t\t CRYPTO \n");
29+
while(rs.next())
30+
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");
31+
32+
stmt = conn.prepareStatement("SELECT * FROM STUDENT WHERE MARKS_JAVA > ? AND MARKS_CRYPTO > ?");
33+
stmt.setDouble(1, 85);
34+
stmt.setDouble(2, 80);
35+
rs = stmt.executeQuery();
36+
System.out.println("\n\nID \t\t NAMES \t\t\t\t\t JAVA \t\t ALGO \t\t CRYPTO \n");
37+
while(rs.next())
38+
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");
39+
40+
}
41+
catch(ClassNotFoundException cnfe)
42+
{
43+
cnfe.printStackTrace();
44+
}
45+
catch(SQLException se)
46+
{
47+
se.printStackTrace();
48+
}
49+
}
50+
51+
}

StudentDatabaseMySQL.java

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import java.sql.*;
2+
import static java.lang.System.out;
3+
import java.util.Scanner;
4+
5+
public class StudentDatabaseMySQL
6+
{
7+
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
8+
static final String JDBC_URL = "jdbc:mysql://localhost:3306/";
9+
static final String DB_NAME = "jdbc";
10+
static final String USERNAME = "root";
11+
static final String PASSWORD = "";
12+
13+
public static void main(String[] args)
14+
{
15+
String createQuery, sName;
16+
ResultSet rs;
17+
int id, choice, rows, i = 0, ids[], maxInd;
18+
double mJava, mAlgo, mCrypto, totalM[], max;
19+
Scanner obj = new Scanner(System.in);
20+
boolean check, dropped = false;
21+
22+
try
23+
{
24+
Class.forName(JDBC_DRIVER);
25+
26+
Connection conn = DriverManager.getConnection(JDBC_URL+DB_NAME,USERNAME,PASSWORD);
27+
28+
Statement stmt = conn.createStatement();
29+
30+
/*
31+
// creating table
32+
createQuery = "CREATE TABLE STUDENT ( " +
33+
"STUDENT_ID INTEGER PRIMARY KEY,"+
34+
"STUDENT_NAME VARCHAR(30),"+
35+
"MARKS_JAVA DOUBLE,"+
36+
"MARKS_ALGO DOUBLE,"+
37+
"MARKS_CRYPTO DOUBLE )";
38+
out.println(createQuery);
39+
stmt.executeUpdate(createQuery);
40+
out.println("STUDENT table successfully created...");
41+
*/
42+
43+
do
44+
{
45+
out.println("\n----MENU----\n1.Display all Records"
46+
+ "\n2. Add a record\n"
47+
+ "3. Delete a record\n"
48+
+ "4. Display records of best student\n"
49+
+ "5. Scale up marks in Algorithm\n"
50+
+ "6. Delete all rows\n"
51+
+ "7. DROP table\n"
52+
+ "8. Exit\n"
53+
+ "Enter choice: ");
54+
choice = obj.nextInt();
55+
switch(choice)
56+
{
57+
case 1: if(dropped)
58+
{
59+
out.println("\nSTUDENT table does not exist in database...\n");
60+
break;
61+
}
62+
createQuery = "SELECT * FROM STUDENT";
63+
rs = stmt.executeQuery(createQuery);
64+
check = false;
65+
if(rs.next())
66+
check = true;
67+
68+
if(check)
69+
{
70+
createQuery = "SELECT * FROM STUDENT";
71+
out.println("\n"+createQuery+"\n");
72+
rs = stmt.executeQuery(createQuery);
73+
System.out.println("\n\nID \t\t NAMES \t\t\t\t\t JAVA \t\t ALGO \t\t CRYPTO \n");
74+
while(rs.next())
75+
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");
76+
}
77+
else
78+
out.println("\nNothing to display...\n");
79+
break;
80+
case 2: if(dropped)
81+
{
82+
out.println("\nSTUDENT table does not exist in database...\n");
83+
break;
84+
}
85+
86+
out.println("\nEnter ID: ");
87+
id = obj.nextInt();
88+
89+
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+id;
90+
rs = stmt.executeQuery(createQuery);
91+
check = false;
92+
93+
if(rs.next())
94+
check = true;
95+
96+
if(check)
97+
out.println("\nID already exists...\n");
98+
else
99+
{
100+
out.println("\nEnter Name: ");
101+
obj.nextLine();
102+
sName = obj.nextLine();
103+
out.println("\nEnter marks in JAVA: ");
104+
mJava = obj.nextDouble();
105+
out.println("\nEnter marks in ALGORITHM: ");
106+
mAlgo = obj.nextDouble();
107+
out.println("\nEnter marks in CRYPTOGRAPHY: ");
108+
mCrypto = obj.nextDouble();
109+
createQuery = "INSERT INTO STUDENT VALUES ( "+id+" , '"+sName+"' , "+mJava+" , "+mAlgo+" , "+mCrypto+" )";
110+
out.println(createQuery);
111+
stmt.executeUpdate(createQuery);
112+
out.println("\nRecord successfully inserted...\n");
113+
}
114+
break;
115+
case 3: if(dropped)
116+
{
117+
out.println("\nSTUDENT table does not exist in database...\n");
118+
break;
119+
}
120+
out.println("\nEnter ID: ");
121+
id = obj.nextInt();
122+
123+
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+id;
124+
rs = stmt.executeQuery(createQuery);
125+
check = false;
126+
127+
if(rs.next())
128+
check = true;
129+
130+
if(!check)
131+
out.println("\nID does not exists...\n");
132+
else
133+
{
134+
createQuery = "DELETE FROM STUDENT WHERE STUDENT_ID = "+id;
135+
out.println(createQuery);
136+
stmt.executeUpdate(createQuery);
137+
out.println("\nRecord successfully deleted...\n");
138+
}
139+
break;
140+
case 4: if(dropped)
141+
{
142+
out.println("\nSTUDENT table does not exist in database...\n");
143+
break;
144+
}
145+
createQuery = "SELECT * FROM STUDENT";
146+
rs = stmt.executeQuery(createQuery);
147+
check = false;
148+
if(rs.next())
149+
check = true;
150+
151+
if(!check)
152+
out.println("\nNo data retrieved...\n");
153+
else
154+
{
155+
rows = 0;
156+
createQuery = "SELECT * FROM STUDENT";
157+
rs = stmt.executeQuery(createQuery);
158+
while(rs.next())
159+
++rows;
160+
161+
totalM = new double[rows];
162+
ids = new int[rows];
163+
164+
createQuery = "SELECT * FROM STUDENT";
165+
rs = stmt.executeQuery(createQuery);
166+
while(rs.next())
167+
{
168+
totalM[i] = rs.getDouble(3) + rs.getDouble(4) + rs.getDouble(5);
169+
ids[i] = rs.getInt(1);
170+
++i;
171+
}
172+
173+
i = 0;
174+
max = totalM[0];
175+
maxInd = ids[0];
176+
for( i = 0; i < totalM.length; ++i )
177+
if( totalM[i] > max )
178+
{
179+
max = totalM[i];
180+
maxInd = ids[i];
181+
}
182+
183+
createQuery = "SELECT * FROM STUDENT WHERE STUDENT_ID = "+maxInd;
184+
out.println(createQuery);
185+
rs = stmt.executeQuery(createQuery);
186+
187+
System.out.println("\n\nID \t\t NAME \n");
188+
while(rs.next())
189+
System.out.print(rs.getInt(1)+"\t\t"+rs.getString(2)+"\n");
190+
}
191+
break;
192+
case 5: if(dropped)
193+
{
194+
out.println("\nSTUDENT table does not exist in database...\n");
195+
break;
196+
}
197+
198+
createQuery = "UPDATE STUDENT SET MARKS_ALGO = MARKS_ALGO + 5 WHERE MARKS_ALGO < 96";
199+
out.println(createQuery);
200+
stmt.executeUpdate(createQuery);
201+
out.println("\nUpdated successfully...\n");
202+
break;
203+
case 6: if(dropped)
204+
{
205+
out.println("\nSTUDENT table does not exist in database...\n");
206+
break;
207+
}
208+
209+
createQuery = "DELETE FROM STUDENT";
210+
out.println(createQuery);
211+
stmt.executeUpdate(createQuery);
212+
out.println("\nSTUDENT table emptied...\n");
213+
break;
214+
case 7: if(!dropped)
215+
{
216+
dropped = true;
217+
createQuery = "DROP TABLE STUDENT";
218+
219+
out.println(createQuery);
220+
stmt.executeUpdate(createQuery);
221+
out.println("STUDENT table successfully dropped...");
222+
}
223+
else
224+
out.println("\nSTUDENT table does not exist in database...\n");
225+
break;
226+
case 8: out.println("\nPLEASURE TO SERVE YOU!!\n");
227+
break;
228+
default: out.println("\nINVALID CHOICE...\n");
229+
}
230+
}
231+
while(choice != 8);
232+
}
233+
catch(ClassNotFoundException cnfe)
234+
{
235+
out.println("Error loading MySQL Driver!");
236+
}
237+
catch(SQLException se)
238+
{
239+
se.printStackTrace();
240+
out.println("Error accessing Database!");
241+
}
242+
}
243+
244+
}

0 commit comments

Comments
 (0)