-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCrud.java
More file actions
142 lines (115 loc) · 4.19 KB
/
Crud.java
File metadata and controls
142 lines (115 loc) · 4.19 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
// Combining all the operations into one program.
package src.college.understanding_DBConnectivity;
import java.sql.*;
import java.io.Console;
public class Crud {
public static void main(String[] args) {
readRecord();
System.out.println("--------------------------\n");
Console c = System.console();
System.out.println("1 - Create the new record");
System.out.println("2 - Display all record");
System.out.println("3 - Update a record");
System.out.println("4 - Delete a record");
System.out.println("5 - Run custom query");
while (true) {
String ch = c.readLine("Enter your choice: ");
switch (ch) {
case "1":
addRecord();
break;
case "2":
readRecord();
break;
case "3":
updateRecord();
break;
case "4":
deleteRecord();
break;
case "5":
customQuery();
break;
default:
System.out.println("Invalid Choice");
}
String again = c.readLine("Perform another query?(y/n): ");
if (again.equals("n")) {
break;
}
}
}
static void connectDB(String query) {
Connection con = null;
String url = "jdbc:mysql://localhost:3306/college_db";
String user = "root";
String password = "";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
try {
if (query != null) {
int i = st.executeUpdate(query);
if (i != 0) {
System.out.println(i + " row affected\n");
readRecord();
} else {
System.out.println(i + " does'nt exists\n");
}
} else {
ResultSet rs = st.executeQuery("SELECT * FROM Students");
displayData(rs);
}
} catch (java.sql.SQLException e) {
ResultSet rs = st.executeQuery(query);
displayData(rs);
}
} catch (Exception e) {
System.out.println(e);
}
}
static void addRecord() {
Console c = System.console();
String id = c.readLine("\nEnter id : ");
String name = c.readLine("Enter student name: ");
String course = c.readLine("Enter student coursename : ");
String query = String.format("INSERT INTO Students VALUES(%s, '%s', '%s')", id, name, course);
connectDB(query);
}
static void readRecord() {
connectDB(null);
}
static void updateRecord() {
Console c = System.console();
String id = c.readLine("\nEnter id : ");
String colName = c.readLine("Enter name of the column: ");
String newValue = c.readLine("Enter new value: ");
String query = String.format("UPDATE Students SET %s = '%s' WHERE id = %s", colName, newValue, id);
connectDB(query);
}
static void deleteRecord() {
Console c = System.console();
String id = c.readLine("\nEnter the id you wish to delete: ");
String query = String.format("DELETE FROM Students WHERE id=%s", id);
connectDB(query);
}
static void customQuery() {
Console c = System.console();
String query = c.readLine("Input query: ");
connectDB(query);
}
static void displayData(ResultSet rs) {
try {
System.out.println("id\tname\tcourse");
System.out.println("--------------------------");
while (rs.next()) {
String data = String.format("%s\t%s\t%s\t", rs.getString("id"), rs.getString("name"),
rs.getString("course"));
System.out.println(data);
}
} catch (Exception e) {
System.out.println(e);
}
}
}