This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P43_JDBC.java
113 lines (104 loc) · 2.67 KB
/
P43_JDBC.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
// Emmanuel Jojy
// Roll no: 53
// S3 CSE A
// JDBC Q2
import java.sql.*;
import java.io.*;
public class P43_JDBC{
Statement st;
ResultSet res;
String query;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
public P43_JDBC(){
try{
Class.forName("org.postgresql.Driver");
Connection c = DriverManager.getConnection("jdbc:postgresql://john.db.elephantsql.com:5432/ghjyduvs", "ghjyduvs", "RN7_ne-JI4lm6NpjTWhQ46-YgiYvFGaJ");
st = c.createStatement();
}
catch(ClassNotFoundException e){
System.out.println("Class Missing. " + e);
System.exit(0);
}
catch(SQLException e){
System.out.println(e);
System.exit(0);
}
}
public void update(){
try{
query = "UPDATE stud SET NAME = 'NEETHU' WHERE ROLL = '12';";
st.executeUpdate(query);
}
catch(SQLException e){
System.out.println(e);
}
System.out.println("\n\tRecord Updated Successfully");
}
public void delete(){
int roll = 0;
try{
query = "DELETE FROM stud WHERE NAME = 'CINI';";
st.executeUpdate(query);
}
catch(SQLException e){
System.out.println("\tCOULD NOT LOCATE 'CINI'.\n\t" + e);
return;
}
System.out.println("\n\tRecord CINI DELETED");
}
public void filter(){
System.out.println("\n\tRank List: ");
try{
query = "SELECT * FROM stud WHERE MARK > 70 ORDER BY MARK DESC;";
res = st.executeQuery(query);
System.out.println("\n\tROLL\tNAME\t\t\tMARK\n");
while(res.next()){
System.out.println("\t" + res.getString("ROLL") + "\t" + res.getString("NAME") + "\t\t" + res.getString("MARK"));
}
}
catch(SQLException e){
System.out.println("");
}
}
public void show(){
System.out.println("\n\tRank List: ");
try{
int r = 1;
query = "SELECT * FROM stud ORDER BY MARK DESC;";
res = st.executeQuery(query);
System.out.println("\n\tRANK\tROLL\tNAME\t\tMARK\n");
while(res.next()){
System.out.println("\t" + r + "\t" + res.getString("ROLL") + "\t" + res.getString("NAME") + "\t\t" + res.getString("MARK"));
r++;
}
}
catch(SQLException e){
System.out.println("");
}
}
public void f(){
while(true){
System.out.println("\n*** STUDENT DB OPTIONS ***");
System.out.println("1. Update Roll 12 to 'NEETHU'\n2. Delete 'cini'\n3. Show greater than 70\n4. Rank List\n5. Exit");
System.out.print("Enter Choice: ");
char ch = '1';
try{
ch = obj.readLine().charAt(0);
}
catch(IOException e){
System.out.println(e);
}
switch(ch){
case '1': update(); break;
case '2': delete(); break;
case '3': filter(); break;
case '4': show(); break;
case '5': return;
}
}
}
public static void main(String[] args){
P43_JDBC stud = new P43_JDBC();
stud.f();
}
}