-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrud.java
166 lines (119 loc) · 4.53 KB
/
Crud.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
package dbConnection;
import java.sql.*;
import java.util.Scanner;
class con {
public static Connection config() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
System.out.println("Connected");
return cn;
} catch (ClassNotFoundException | SQLException e) {
throw new RuntimeException(e);
}
}
}
public class Crud {
static Connection connection = con.config();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter 0 => exit & 1 => Continue: ");
int out = sc.nextInt();
while (out != 0) {
System.out.print("Enter 1 => Insert || " +
"Enter 2 => Select:\n" +
"Enter 3 => Delete || " +
"Enter 4 => Update:\n \t Here=>");
int in = sc.nextInt();
switch (in) {
case 1: // Insert
con.config();
Fun_insert();
break;
case 2: // Select
con.config();
Fun_select();
break;
case 3: // Delete
con.config();
Fun_Delete();
break;
case 4: // Update
con.config();
Fun_Update();
break;
default:
throw new IllegalStateException("Unexpected value: " + in);
}
System.out.print("Enter 0 => exit & 1 => Continue: ");
out = sc.nextInt();
}
}
private static void Fun_Update() {
try {
Scanner read1 = new Scanner(System.in);
Scanner read2 = new Scanner(System.in);
Scanner read3 = new Scanner(System.in);
System.out.print("Enter Id: ");
int id = read1.nextInt();
System.out.print("Enter Username: ");
String unm = read3.nextLine();
System.out.print("Enter Password: ");
String pwd = read2.nextLine();
String update = "UPDATE admin set unm='" + unm + "' , pwd='" + pwd + "' where id=" + id;
Statement st = connection.createStatement();
st.executeUpdate(update);
st.close();
System.out.println("Record Updated!!!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static void Fun_Delete() {
try {
Scanner inId = new Scanner(System.in);
System.out.print("Enter Id: ");
int id = inId.nextInt();
String delete = "delete from admin where id=" + id;
Statement st = connection.createStatement();
st.executeUpdate(delete);
st.close();
System.out.println("Record Deleted!!!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static void Fun_select() {
try {
Statement st = connection.createStatement();
String select = "Select * from admin";
ResultSet rs = st.executeQuery(select);
while (rs.next()) {
int id = rs.getInt("id");
String unm = rs.getString("unm");
String pwd = rs.getString("pwd");
System.out.println("Id: " + id + "\t\t User Name: " + unm + "\t\t Password: " + pwd);
}
rs.close();
st.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void Fun_insert() {
try {
Scanner read = new Scanner(System.in);
System.out.print("Enter Username: ");
String unm = read.nextLine();
System.out.print("Enter Password: ");
String pwd = read.nextLine();
String insert = "insert into admin(unm , pwd) values('" + unm + "' , '" + pwd + "')";
Statement st = connection.createStatement();
st.executeUpdate(insert);
st.close();
System.out.println("Record inserted!!!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}