From 80a2dffd1873395f04ed949162a86066a309ec6a Mon Sep 17 00:00:00 2001 From: Frantatita Date: Thu, 16 May 2024 11:38:41 -0600 Subject: [PATCH] DAO Examen --- .../java/mx/uv/Controller/DAO_Examen.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 backend/src/main/java/mx/uv/Controller/DAO_Examen.java diff --git a/backend/src/main/java/mx/uv/Controller/DAO_Examen.java b/backend/src/main/java/mx/uv/Controller/DAO_Examen.java new file mode 100644 index 0000000..bac743b --- /dev/null +++ b/backend/src/main/java/mx/uv/Controller/DAO_Examen.java @@ -0,0 +1,109 @@ +package mx.uv.Controller; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import mx.uv.Model.Examen; + +public class DAO_Examen { + + private static Conexion cn = Conexion.getInstance(); + + public static List dameExamenes(){ + Statement stm = null; + ResultSet rs = null; + Connection conn = null; + List resultado = new ArrayList<>(); + + conn = cn.conectar(); + + try { + String sql = "SELECT * from examen"; + stm = conn.createStatement(); + rs = stm.executeQuery(sql); + while (rs.next()) { + Examen u = new Examen(rs.getInt(1),rs.getDouble(2),rs.getBoolean(3),rs.getInt(4)); + resultado.add(u); + } + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(null, conn); + } + return resultado; + } + + + public static boolean agregarExamen(Examen examen){ + PreparedStatement stm = null; + Connection conn = null; + boolean msj = false; + + conn = cn.conectar(); + + try{ + String sql = "INSERT INTO `examen` (`calificacion`, `derechoInscripcion`) VALUES(?,?);"; + stm = (PreparedStatement) conn.prepareStatement(sql); + stm.setDouble(1, examen.getCalificacion()); + stm.setBoolean(2, examen.isDerechoInscripcion()); + + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(stm, conn); + } + return msj; + } + + + private static void cerrarConexiones(PreparedStatement stm, Connection conn) { + if(stm != null){ + try{ + stm.close(); + }catch(Exception e){ + System.out.println(e); + } + stm = null; + } + try{ + conn.close(); + cn.cerrarConexion(); + } catch(Exception e){ + System.out.println(e); + } + } + + public static boolean eliminarExamen(int id){ + return false; + } + + public static boolean editarExamen(Examen examen){ + PreparedStatement stm = null; + Connection conn = null; + boolean verificacion = false; + conn = cn.conectar(); + + try{ + String sql = "UPDATE `examen` SET `calificacion` = ?, `derechoInscripcion` = ? WHERE `idExamen` = ?;"; + stm = conn.prepareStatement(sql); + stm.executeQuery(); + verificacion = true; + } catch (SQLException ex){ + System.err.println(ex); + } finally{ + cerrarConexiones(stm, conn); + cn.cerrarConexion(); + } + + return verificacion; + } + + + + +}