BootcampDao.java 4.73 KB
Newer Older
1 2 3 4 5
package com.roshka.proyectofinal.bootcamp;

import com.roshka.proyectofinal.DataBase;
import com.roshka.proyectofinal.entity.Bootcamp;

6 7 8
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

public class BootcampDao {

    public static int save(Bootcamp b){
        int status=0;

        try{
            Connection con= DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement(
                    "insert into bootcamp (id_lenguaje,id_profesor,fecha_inicio,fecha_fin,descripcion,imagen,titulo,activo) values (?,?,?,?,?,?,?,?)");
            ps.setInt(1,b.getId_lenguaje());
            ps.setInt(2,b.getId_profesor());
            ps.setString(3,b.getFecha_inicio());
            ps.setString(4,b.getFecha_fin());
            ps.setString(5,b.getDescripcion());
            ps.setString(6,b.getImagen());
            ps.setString(7,b.getTitulo());
            ps.setBoolean(8,b.getActivo());

            status=ps.executeUpdate();

            con.close();
        }catch(Exception ex){ex.printStackTrace();}

        return status;
    }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    public static int update(Bootcamp b){
        int status=0;
        try{
            Connection con= DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement(
                    "update Bootcamp set id_lenguaje=?,id_profesor=?,fecha_inicio=?,fecha_fin=?,descripcion=?,titulo=?,activo=? where id=?");
            ps.setInt(1,b.getId_lenguaje());
            ps.setInt(2,b.getId_profesor());
            ps.setString(3,b.getFecha_inicio());
            ps.setString(4,b.getFecha_fin());
            ps.setString(5,b.getDescripcion());
            ps.setString(6,b.getTitulo());
            ps.setBoolean(7,b.getActivo());
            ps.setInt(8,b.getId());

            status=ps.executeUpdate();

            con.close();
        }catch(Exception ex){ex.printStackTrace();}

        return status;
    }


    public static List<Bootcamp> listar(){
        ArrayList<Bootcamp> list = new ArrayList<>();
        String sql = "select a.id, a.fecha_inicio, a.fecha_fin, a.descripcion, a.titulo,\n" +
                "a.activo, b.nombre_lenguaje, c.nombre, c.apellido \n" +
                "from bootcamp a\n" +
                "inner join lenguaje b\n" +
                "on a.id_lenguaje=b.id\n" +
                "inner join profesor c\n" +
                "on a.id_profesor=c.id";

        try{
            Connection con= DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();


            while(rs.next()){
                Bootcamp boot = new Bootcamp();

                boot.setId(rs.getInt("id"));
                boot.setActivo(rs.getBoolean("activo"));
                boot.setDescripcion(rs.getString("descripcion"));
                boot.setTitulo(rs.getString("titulo"));
                boot.setFecha_fin(rs.getString("fecha_fin"));
                boot.setFecha_inicio(rs.getString("fecha_inicio"));
                boot.setNombre_profesor(rs.getString("nombre"));
                boot.setApellido_profesor(rs.getString("apellido"));
                boot.setNombre_lenguaje(rs.getString("nombre_lenguaje"));

                list.add(boot);
            }

            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    public static int delete(int id){
        int status=0;
        try{
            Connection con=DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement("delete from bootcamp where id=?");
            ps.setInt(1,id);
            status=ps.executeUpdate();

            con.close();
        }catch(Exception e){e.printStackTrace();}

        return status;
    }


114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    public static Bootcamp getBootcampById(int id){
        Bootcamp b=new Bootcamp();

        try{
            Connection con=DataBase.getConnection();
            PreparedStatement ps=con.prepareStatement("select * from bootcamp where id=?");
            ps.setInt(1,id);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                b.setId(rs.getInt("id"));
                b.setActivo(rs.getBoolean("activo"));
                b.setDescripcion(rs.getString("descripcion"));
                b.setTitulo(rs.getString("titulo"));
                b.setFecha_fin(rs.getString("fecha_fin"));
                b.setFecha_inicio(rs.getString("fecha_inicio"));
                b.setNombre_profesor(rs.getString("nombre"));
                b.setApellido_profesor(rs.getString("apellido"));
                b.setNombre_lenguaje(rs.getString("nombre_lenguaje"));
                b.setImagen(rs.getString("imagen"));
            }
            con.close();
        }catch(Exception ex){ex.printStackTrace();}

        return b;
    }
139
}