Dao Administrador

This commit is contained in:
dangj501 2024-05-09 23:13:25 -06:00
parent 8d1d8db6a0
commit 41b0f2c3aa
2 changed files with 131 additions and 2 deletions

View File

@ -0,0 +1,129 @@
package mx.uv.Controller;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import mx.uv.Model.Administrador;
public class DAO_Administrador{
private static Conexion cn = Conexion.getInstance();
public static List<Administrador> dameAministradores(){
Statement stm = null;
ResultSet rs = null;
Connection conn = null;
List<Administrador> resultado = new ArrayList<>();
conn = cn.conectar();
try {
String sql = "SELECT * from administrador";
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
Administrador u = new Administrador(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4),rs.getString(5));
resultado.add(u);
}
} catch (Exception e) {
System.out.println(e);
} finally {
cerrarConexiones(null, conn);
}
return resultado;
}
public boolean agregarAdministrador(Administrador administrador){
PreparedStatement stm = null;
Connection conn = null;
boolean msj= false;
conn = cn.conectar();
try{
String sql ="INSERT INTO `administrador` (`matricula`,`contrasena`, `nombre`, `apellido`) VALUES(?,?,?,?);";
stm = (PreparedStatement) conn.prepareStatement(sql);
stm.setString(1, administrador.getMatricula());
stm.setString(2, administrador.getContrasena());
stm.setString(3, administrador.getNombre());
stm.setString(4, administrador.getNombre());
stm.setString(5, administrador.getApellido());
}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 eliminarAdministrador(int idAdministrador){
return false;
}
public static boolean editarAdministrador(Administrador administrador){
PreparedStatement stm = null;
Connection conn = null;
boolean verificacion =false;
conn = cn.conectar();
try{
String sql ="UPDATE `administrador` SET `matricula` = '"+administrador.getMatricula()+"',`contrasena` = '"+administrador.getContrasena()+"',`nombre` = '"+administrador.getNombre()+"',`apellido` = '"+administrador.getApellido()+"' WHERE `id` = '"+administrador.getId()+"';";
stm = conn.prepareStatement(sql);
stm.executeUpdate();
verificacion = true;
}catch (SQLException ex) {
System.err.println(ex);
}finally{
cerrarConexiones(stm, conn);
cn.cerrarConexion();
}
return verificacion;
}
public static Administrador administradorIniciado(String matricula, String contrasena){
ResultSet rs = null;
Connection conn = null;
Administrador administrador =null;
conn = cn.conectar();
try {
String sql = "SELECT * FROM administrador WHERE matricula = ? AND contrasena = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, matricula);
stmt.setString(2, contrasena);
rs = stmt.executeQuery();
while (rs.next()) {
administrador = new Administrador(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
}
} catch (Exception e) {
System.out.println(e);
} finally {
cerrarConexiones(null, conn);
}
return administrador;
}
}

View File

@ -43,7 +43,7 @@ public class DAO_Carrrera{
conn = cn.conectar();
try{
String sql ="INSERT INTO `carrera` (`area`,`nombre`, `modalidad`, `campus`, `costo`) VALUES(?,?,?,?);";
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());
@ -101,6 +101,6 @@ public class DAO_Carrrera{
}
return verificacion;
}
}