-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conexion.java
75 lines (60 loc) · 1.53 KB
/
Conexion.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.sql.*;
public class Conexion {
private String host = "jdbc:mysql://localhost:3306/laberinto";
private String user = "root";
private String password = "";
// CONECTA A LA BASE DE DATOS
public Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(host, user, password);
} catch (SQLException ex) {
System.out.println ("¡Error al conectarse a la base de datos!");
}
return conn;
}
// MUESTRA RESULTADOS
public void mostrarResultados(String query) {
try {
Statement cursor = this.getConn().createStatement();
ResultSet consulta = cursor.executeQuery(query);
while (consulta.next()) {
int id = consulta.getInt("id");
String nombre = consulta.getString("nombre");
System.out.println(id + " " + nombre);
}
consulta.close();
cursor.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// INSERTA DATOS
public void insertarDatos(String query) {
try {
Statement cursor = this.getConn().createStatement();
cursor.executeUpdate(query);
cursor.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// ELIMINA DATOS
public void eliminarDatos(String query) {
try {
Statement cursor = this.getConn().createStatement();
cursor.executeUpdate(query);
cursor.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// CIERRA CONEXION
public void closeConn() {
try {
this.getConn().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}