From 701b5c0e1d1c875504e36fdc8b3b2473b26e35dd Mon Sep 17 00:00:00 2001 From: Frantatita Date: Thu, 9 May 2024 09:55:22 -0600 Subject: [PATCH] DAOTutor --- .../main/java/mx/uv/Controller/DAOTutor.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 backend/src/main/java/mx/uv/Controller/DAOTutor.java diff --git a/backend/src/main/java/mx/uv/Controller/DAOTutor.java b/backend/src/main/java/mx/uv/Controller/DAOTutor.java new file mode 100644 index 0000000..c9278ce --- /dev/null +++ b/backend/src/main/java/mx/uv/Controller/DAOTutor.java @@ -0,0 +1,127 @@ +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.Tutor; + + +public class DAOTutor { + + private static Conexion cn = Conexion.getInstance(); + + public static List dameTutores() { + Statement stm = null; + ResultSet rs = null; + Connection conn = null; + List resultado = new ArrayList<>(); + + conn = cn.conectar(); + + try { + String sql = "SELECT * from tutor"; + stm = conn.createStatement(); + rs = stm.executeQuery(sql); + while (rs.next()) { + Tutor u = new Tutor(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6)); + resultado.add(u); + } + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(null, conn); + } + return resultado; + } + + public static boolean validarTutor(Tutor tutor){ + Statement stm = null; + Connection conn = null; + boolean verificacion =false; + ResultSet rs = null; + conn = cn.conectar(); + try { + String sql ="select * from tutor " + + "where id= '"+tutor.getId()+"' and nombre='"+tutor.getNombre()+"'"; + stm = (Statement) conn.createStatement(); + rs = stm.executeQuery(sql); + if(rs.next()){ + verificacion = true; + }else{ + verificacion = false; + } + conn.close(); + } catch (Exception ex) { + System.err.println(ex); + } + return verificacion; + } + + public static boolean agregarTutor(Tutor tutor) { + PreparedStatement stm = null; + Connection conn = null; + boolean msj = false; + + conn = cn.conectar(); + try { + String sql = "INSERT INTO `tutor`(`nombre`,`apellido`,`parentesco`,`ocupacion`,`telefono`)VALUES(?,?,?,?,?);"; + stm = (PreparedStatement) conn.prepareStatement(sql); + stm.setString(1, tutor.getNombre()); + stm.setString(2, tutor.getApellido()); + stm.setString(3, tutor.getParentesco()); + stm.setString(4, tutor.getOcupacion()); + stm.setString(5, tutor.getTelefono()); + } 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 eliminarTutor(Tutor tutor) { + return false; + } + + public static boolean editarTutor(Tutor tutor) { + PreparedStatement stm = null; + Connection conn = null; + boolean verificacion = false; + conn = cn.conectar(); + try{ + String sql ="UPDATE `tutor` SET `nombre` = '"+tutor.getNombre()+"',`apellido` = '"+tutor.getApellido()+"',`parentesco` = '"+tutor.getParentesco()+"',`ocupacion` = '"+tutor.getOcupacion()+"', `telefono` = '"+tutor.getTelefono()+"';"; + stm = conn.prepareStatement(sql); + stm.executeQuery(); + verificacion = true; + } catch (SQLException ex){ + System.out.println(ex); + } finally { + cerrarConexiones(stm, conn); + cn.cerrarConexion(); + } + return verificacion; + } + +}