-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdbcInsertDemo.java
More file actions
59 lines (44 loc) · 1.39 KB
/
Copy pathjdbcInsertDemo.java
File metadata and controls
59 lines (44 loc) · 1.39 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 jdbcInsertDemo {
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();
// 3. Insertando un nuevo registro de cliente
System.out.println("Insertando un nuevo registro\n");
int rowsAffected = myStamt.executeUpdate(
"insert into clientes " +
"(primer_nombre, ap_paterno, ap_materno, email, saldo) " +
"values " +
"('Ana', 'Almaraz', 'Moran', 'anna.m@platzi.com', 2000.00)");
// 4. Verificando obteniendo la lista
myRes = myStamt.executeQuery("select * from clientes order by ap_paterno");
// 5. Procesando el resultado imprimiendo apellido y nombre
while (myRes.next()) {
System.out.println(myRes.getString("ap_paterno") + ", " + myRes.getString("primer_nombre"));
}
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myRes != null) {
myRes.close();
}
if (myStamt != null) {
myStamt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
}