1
+ import java .util .Scanner ;
2
+
3
+ import java .sql .*;
4
+
5
+ class StudentVIT {
6
+ String name ;
7
+ String password ;
8
+ String school ;
9
+ int marks ;
10
+
11
+ void getDetails () {
12
+ Scanner sc = new Scanner (System .in );
13
+ System .out .print ("Enter your name: " );
14
+ name = sc .nextLine ();
15
+ System .out .print ("Enter your password: " );
16
+ password = sc .nextLine ();
17
+ System .out .println ("Enter School: " );
18
+ school = sc .nextLine ();
19
+ System .out .println ("Enter marks" );
20
+ marks = sc .nextInt ();
21
+ }
22
+
23
+ void insertRecord () throws SQLException , InstantiationException , IllegalAccessException , ClassNotFoundException {
24
+ try {
25
+ DatabaseConn dbms = new DatabaseConn ("jdbc://mysql://localhost:3306/vit" , "root" , "" );
26
+ Connection conn = dbms .getConnection ();
27
+ String sql = "INSERT INTO student VALUES (?,?,?,?)" ;
28
+ PreparedStatement stmt = conn .prepareStatement (sql );
29
+ stmt .setString (1 , name );
30
+ stmt .setString (2 , password );
31
+ stmt .setString (3 , school );
32
+ stmt .setInt (4 , marks );
33
+ stmt .execute ();
34
+ System .out .println ("Record inserted successfully" );
35
+ dbms .closeConnection (conn , stmt );
36
+ }
37
+ catch (Exception e ) {
38
+ e .printStackTrace ();
39
+ }
40
+ }
41
+
42
+ void updateRecord () throws SQLException , InstantiationException , IllegalAccessException , ClassNotFoundException {
43
+ try {
44
+ DatabaseConn dbms = new DatabaseConn ("jdbc://mysql://localhost:3306/vit" , "root" , "" );
45
+ Connection conn = dbms .getConnection ();
46
+ System .out .println ("Enter your name: " );
47
+ Scanner sc = new Scanner (System .in );
48
+ String inputname = sc .nextLine ();
49
+ System .out .println ("Enter your new password: " );
50
+ String inputpass = sc .nextLine ();
51
+
52
+ String sql = "UPDATE student SET password = ? WHERE name = ?" ;
53
+ PreparedStatement stmt = conn .prepareStatement (sql );
54
+ stmt .setString (1 , inputname );
55
+ stmt .setString (2 , inputpass );
56
+ int i = stmt .executeUpdate ();
57
+ if (i >0 ) {
58
+ System .out .println ("Record updated successfully" );
59
+ }
60
+ else {
61
+ System .out .println ("No such record found in the database" );
62
+ }
63
+ dbms .closeConnection (conn , stmt );
64
+ }
65
+ catch (Exception e ) {
66
+ e .printStackTrace ();
67
+ }
68
+ }
69
+
70
+ void deleteRecord () throws SQLException , InstantiationException , IllegalAccessException , ClassNotFoundException {
71
+ try {
72
+ DatabaseConn dbms = new DatabaseConn ("jdbc://mysql://localhost:3306/vit" , "root" , "" );
73
+ Connection conn = dbms .getConnection ();
74
+ System .out .println ("Enter name to be deleted: " );
75
+ Scanner sc = new Scanner (System .in );
76
+ String inputname = sc .nextLine ();
77
+
78
+ String sql = "DELETE FROM student WHERE name = ?" ;
79
+ PreparedStatement stmt = conn .prepareStatement (sql );
80
+ stmt .setString (1 , inputname );
81
+ int i = stmt .executeUpdate ();
82
+ if (i >0 ) {
83
+ System .out .println ("Record deleted successfully" );
84
+ }
85
+ else {
86
+ System .out .println ("No such record found in the database" );
87
+ }
88
+ dbms .closeConnection (conn , stmt );
89
+ }
90
+ catch (Exception e ) {
91
+ e .printStackTrace ();
92
+ }
93
+ }
94
+
95
+ void searchRecord () throws SQLException , InstantiationException , IllegalAccessException , ClassNotFoundException {
96
+ try {
97
+ DatabaseConn dbms = new DatabaseConn ("jdbc://mysql://localhost:3306/vit" , "root" , "" );
98
+ Connection conn = dbms .getConnection ();
99
+ System .out .println ("Enter name to be searched: " );
100
+ Scanner sc = new Scanner (System .in );
101
+ String inputname = sc .nextLine ();
102
+
103
+ String sql = "SELECT * FROM student WHERE name = ?" ;
104
+ PreparedStatement stmt = conn .prepareStatement (sql , ResultSet .TYPE_SCROLL_INSENSITIVE , ResultSet .CONCUR_UPDATABLE );
105
+ stmt .setString (1 , inputname );
106
+ ResultSet rs = stmt .executeQuery ();
107
+ if (rs .next () == false ) {
108
+ System .out .println ("No such record found in the database" );
109
+ }
110
+ else {
111
+ rs .previous ();
112
+ while (rs .next ()) {
113
+ System .out .println ("Name: " + rs .getString (1 ));
114
+ System .out .println ("Password: " + rs .getString (2 ));
115
+ System .out .println ("School: " + rs .getString (3 ));
116
+ System .out .println ("Marks: " + rs .getString (4 ));
117
+ }
118
+ }
119
+ dbms .closeConnection (conn , stmt );
120
+ }
121
+ catch (Exception e ) {
122
+ e .printStackTrace ();
123
+ }
124
+ }
125
+
126
+ }
127
+
128
+ class DatabaseConn {
129
+ String url ;
130
+ String username ;
131
+ String password ;
132
+
133
+ DatabaseConn (String url , String username , String password ) {
134
+ this .url = url ;
135
+ this .username = username ;
136
+ this .password = password ;
137
+ }
138
+
139
+ public Connection getConnection () throws InstantiationException , IllegalAccessException , ClassNotFoundException , SQLException {
140
+ Connection conn = null ;
141
+ Class .forName ("com.mysql.kela.jdbc.Driver" ).newInstance ();
142
+ conn = DriverManager .getConnection (url , username , password );
143
+ System .out .println ("Connection is established" );
144
+ return conn ;
145
+ }
146
+
147
+ public void closeConnection (Connection conn , Statement stmt ) throws SQLException {
148
+ stmt .close ();
149
+ conn .close ();
150
+ }
151
+ }
152
+
153
+ public class AppJDBC {
154
+ public static void main (String [] args ) {
155
+ Scanner sc = new Scanner (System .in );
156
+ int ch ;
157
+ try {
158
+ do {
159
+ System .out .println ("Select a choice.\n 1. Enter Student Details\n 2. Update Password\n 3. Delete Student\n 4. Search Record\n 5. Exit" );
160
+ ch = 0 ;
161
+ StudentVIT s = new StudentVIT ();
162
+ System .out .print ("Enter choice: " );
163
+ ch = sc .nextInt ();
164
+ switch (ch )
165
+ {
166
+ case 1 :
167
+ s .getDetails ();
168
+ s .insertRecord ();
169
+ break ;
170
+
171
+ case 2 :
172
+ s .updateRecord ();
173
+ break ;
174
+
175
+ case 3 :
176
+ s .deleteRecord ();
177
+ break ;
178
+
179
+ case 4 :
180
+ s .searchRecord ();
181
+ break ;
182
+
183
+ case 5 :
184
+ System .out .println ("Exiting.." );
185
+ break ;
186
+
187
+ default :
188
+ System .out .println ("Please select the correct option." );
189
+ break ;
190
+ }
191
+ }while (ch !=5 );
192
+ }
193
+ catch (Exception e ) {
194
+ e .printStackTrace ();
195
+ }
196
+ }
197
+ }
0 commit comments