MCPcopy Create free account
hub / github.com/JerePrograma/FullStackEgg / DAO

Class DAO

Etapa3JDBC/Tienda/src/tienda/persistencia/DAO.java:9–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import java.sql.Statement;
8
9public abstract class DAO {
10
11 protected Connection conexion = null;
12 protected ResultSet resultado = null;
13 protected Statement sentencia = null;
14
15 private final String USER = "root";
16 private final String PASSWORD = "root";
17 private final String DATABASE = "tienda";
18 private final String DRIVER = "com.mysql.jdbc.Driver";
19
20 protected void conectarBase() throws ClassNotFoundException, SQLException {
21 try {
22 Class.forName(DRIVER);
23 String url = "jdbc:mysql://localhost:3306/" + DATABASE + "?useSSL=false";
24 conexion = DriverManager.getConnection(url, USER, PASSWORD);
25
26 } catch (ClassNotFoundException | SQLException e) {
27 throw e;
28 }
29 }
30
31 protected void desconectarBase() throws Exception {
32 try {
33 if (resultado != null) {
34 resultado.close();
35 }
36 if (sentencia != null) {
37 sentencia.close();
38 }
39 if (conexion != null) {
40 conexion.close();
41 }
42 } catch (Exception e) {
43 throw e;
44 }
45 }
46
47 protected void insertarModificarEliminar(String sql) throws Exception {//ES LA CONSULA
48 try {
49 conectarBase();//esto es por si está desconectado, la conecta
50 sentencia = conexion.createStatement();
51 sentencia.executeUpdate(sql);
52 //Acá estamos diciendo que esta sentencia, va a usar la consulta SQL que tiene por parámetro
53 //INSERT INTO ....... ETC
54 } catch (ClassNotFoundException | SQLException e) {
55 throw e;
56 } finally {
57 desconectarBase();
58 }
59 }
60
61 protected void consultarBase(String sql) throws Exception {
62 try {
63 conectarBase();
64 sentencia = conexion.createStatement();
65 resultado = sentencia.executeQuery(sql);
66 //Select nombre from producto

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected