-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestErrores.java
More file actions
57 lines (42 loc) · 1.29 KB
/
Copy pathtestErrores.java
File metadata and controls
57 lines (42 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
import java.sql.*; // Importamos para poder interactuar con la BD
public class jdbcTest {
public static void main(String[] args) throws SQLException {
// Declaracion de mis 3 variables
Connection myConn = null;
Statement myStamt = null;
ResultSet myRes = null;
// Guardando mi información de conexión en 3 variables
String url = "jdbc:mysql://localhost:3306/proyecto";
String usuario = "platzi";
String pass = "platzi";
try {
// 1. Conexion a nuestra base de datos, pasando 3 argumentos BD/usuario/contraseña
myConn = DriverManager.getConnection(url, usuario , pass);
System.out.println("Genial, nos hemos conectado\n");
// 2. Crear una declaracion objeto
myStamt = myConn.createStatement();
// 3. Ejecutar consulta SQL
myRes = myStamt.executeQuery("select * from clientes");
// 4. Procesar los resultados
while (myRes.next()) {
System.out.println(myRes.getString("primer_nombre"));
}
}
catch (Exception exc) {
exc.printStackTrace();
System.out.println("Algo salio mal): \n");
}
finally {
//Cerramos los objetos para liberar los recursos
if (myRes != null) {
myRes.close();
}
if (myStamt != null) {
myStamt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
}