DAO Examen

This commit is contained in:
Frantatita 2024-05-16 11:38:41 -06:00
parent b582c3b673
commit 80a2dffd18
1 changed files with 109 additions and 0 deletions

View File

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