-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdbcUpdateDemo.java
More file actions
59 lines (43 loc) · 1.29 KB
/
Copy pathjdbcUpdateDemo.java
File metadata and controls
59 lines (43 loc) · 1.29 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
import java.sql.*;
public class jdbcUpdateDemo {
public static void main(String[] args) throws SQLException {
Connection myConn = null;
Statement myStamt = null;
ResultSet myRes = null;
String url = "jdbc:mysql://localhost:3306/proyecto";
String usuario = "platzi";
String pass = "platzi";
try {
// 1. Establecer conexion a la base de datos
myConn = DriverManager.getConnection(url, usuario, pass);
// 2. Crear la declaracion de objeto Statement
myStamt = myConn.createStatement();
// UPDATE
System.out.println("\nEjecutando cambios en cliente: \n");
int rowsAffected = myStamt.executeUpdate(
"update clientes " +
"set email='maria_gut@platzi.com' " +
"where primer_nombre='Maria' and ap_paterno='Gutierrez'");
// 4. Verificando obteniendo la lista
myRes = myStamt.executeQuery("select * from clientes order by ap_paterno");
// 5. Procesando el resultado imprimiendo apellido y email
while (myRes.next()) {
System.out.println(myRes.getString("ap_paterno") + ", " + myRes.getString("email"));
}
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRes != null) {
myRes.close();
}
if (myStamt != null) {
myStamt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
}