-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate.java
35 lines (32 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
import java.sql.*;
public class Update {
public static void main(String args[]) throws ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "System@321sql";
String query = "UPDATE employees\n" + "SET job = 'Java Developer', salary = 7700000.0\n" + "WHERE id = 1;";
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded successfully");
} catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection con = DriverManager.getConnection(url, username, password);
System.out.println("connection established successfully");
Statement st = con.createStatement();
int rowsaffected = st.executeUpdate(query);
if(rowsaffected>0) {
System.out.println("Updation Successfully"+rowsaffected+ "row(s) affected.");
}else {
System.out.println("Updation Failed..");
}
st.close();
con.close();
System.out.println();
System.out.println("connection closed successfully!!!");
} catch(SQLException e) {
System.out.println(e.getMessage());
}
}
}