-
Notifications
You must be signed in to change notification settings - Fork 9
/
ConexionSingleton.java
44 lines (37 loc) · 969 Bytes
/
ConexionSingleton.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Clase usada para generar un objeto Connection a base de datos MySQL siguiendo el patrón Singleton
* @author Inazio
*
*/
public class ConexionSingleton {
// Propiedades
private static Connection CONEXION = null;
// Constructor
private ConexionSingleton() {
String url = "jdbc:mysql://localhost:3306/test";
String driver = "com.mysql.jdbc.Driver";
String usuario = "root";
String password = "";
try {
Class.forName(driver);
CONEXION = DriverManager.getConnection(url, usuario, password);
}
catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// Métodos
/**
* Devuelve la conexión a la base de datos
* @return Un objeto Connection único para toda la aplicación
*/
public static Connection devolverConexion() {
if (CONEXION == null) {
new ConexionSingleton();
}
return CONEXION;
}
}