From 7f5aadd12859c63385b094efc9b518dc482a258a Mon Sep 17 00:00:00 2001 From: dangj501 Date: Thu, 9 May 2024 09:37:02 -0600 Subject: [PATCH] DAO_Carrera --- .../java/mx/uv/Controller/DAO_Carrrera.java | 106 ++++++++++++++++++ .../src/main/java/mx/uv/Model/Carrera.java | 2 +- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/mx/uv/Controller/DAO_Carrrera.java diff --git a/backend/src/main/java/mx/uv/Controller/DAO_Carrrera.java b/backend/src/main/java/mx/uv/Controller/DAO_Carrrera.java new file mode 100644 index 0000000..fbe6983 --- /dev/null +++ b/backend/src/main/java/mx/uv/Controller/DAO_Carrrera.java @@ -0,0 +1,106 @@ +package mx.uv.Controller; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + + +import mx.uv.Model.Carrera; + +public class DAO_Carrrera{ + private static Conexion cn = Conexion.getInstance(); + + public static List dameCarreras(){ + Statement stm = null; + ResultSet rs = null; + Connection conn = null; + List resultado = new ArrayList<>(); + + conn = cn.conectar(); + + try { + String sql = "SELECT * from carrera"; + stm = conn.createStatement(); + rs = stm.executeQuery(sql); + while (rs.next()) { + Carrera u = new Carrera(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getDouble(6)); + resultado.add(u); + } + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(null, conn); + } + return resultado; + + } + + public static boolean agregarCarrera(Carrera carrera){ + PreparedStatement stm = null; + Connection conn = null; + boolean msj= false; + + conn = cn.conectar(); + + try{ + String sql ="INSERT INTO `carrera` (`area`,`nombre`, `modalidad`, `campus`, `costo`) VALUES(?,?,?,?);"; + stm = (PreparedStatement) conn.prepareStatement(sql); + stm.setString(1, carrera.getArea()); + stm.setString(2, carrera.getNombre()); + stm.setString(3, carrera.getModalidad()); + stm.setString(4, carrera.getCampus()); + stm.setDouble(5, carrera.getCosto()); + + + + }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 eliminarCarrera(int idCarrera){ + return false; + } + + public static boolean editarCarrera(Carrera carrera){ + PreparedStatement stm = null; + Connection conn = null; + boolean verificacion =false; + conn = cn.conectar(); + + try{ + String sql ="UPDATE `alumno` SET `area` = '"+carrera.getArea()+"',`nombre` = '"+carrera.getNombre()+"',`modalidad` = '"+carrera.getModalidad()+"',`campus` = '"+carrera.getCampus()+"', `costo` = '"+carrera.getCosto()+"' WHERE `id` = '"+carrera.getId()+"';"; + stm = conn.prepareStatement(sql); + stm.executeUpdate(); + verificacion = true; + }catch (SQLException ex) { + System.err.println(ex); + }finally{ + cerrarConexiones(stm, conn); + cn.cerrarConexion(); + } + return verificacion; + } + + +} \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/Model/Carrera.java b/backend/src/main/java/mx/uv/Model/Carrera.java index 541f44e..394512a 100644 --- a/backend/src/main/java/mx/uv/Model/Carrera.java +++ b/backend/src/main/java/mx/uv/Model/Carrera.java @@ -61,7 +61,7 @@ public class Carrera { return campus; } - public double getCarrera(){ + public double getCosto(){ return costo; }