DAO_Carrera

This commit is contained in:
dangj501 2024-05-09 09:37:02 -06:00
parent 25d48af99e
commit 7f5aadd128
2 changed files with 107 additions and 1 deletions

View File

@ -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<Carrera> dameCarreras(){
Statement stm = null;
ResultSet rs = null;
Connection conn = null;
List<Carrera> 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;
}
}

View File

@ -61,7 +61,7 @@ public class Carrera {
return campus; return campus;
} }
public double getCarrera(){ public double getCosto(){
return costo; return costo;
} }