-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.java
49 lines (38 loc) · 1.35 KB
/
update.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
package jdbcdemo;
import java.io.*;
import java.sql.*;
import java.util.*;
public class update {
public static void main(String[] args) throws IOException {
String url = "jdbc:mysql://localhost:3306/demo";
String user = "root";
String password = "rootroot";
Connection myConn = null;
Statement myStmt = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Id no to be update:");
int uid=Integer.parseInt(in.readLine());
System.out.println("Enter updated email Id");
String str=in.readLine();
try {
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
myStmt = myConn.createStatement();
String sql = "update employees set email='"+str+"' where id='"+uid+"'";
int rowsAffected = myStmt.executeUpdate(sql);
System.out.println("Rows affected: " + rowsAffected);
System.out.println("Update complete.");
}
catch (Exception exc) {
exc.printStackTrace();
}
/* finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}*/
}
}