This commit is contained in:
Frantatita 2024-05-09 09:55:22 -06:00
parent 8cca97e065
commit 701b5c0e1d
1 changed files with 127 additions and 0 deletions

View File

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