diff --git a/src/main/java/com/roshka/proyectofinal/bootcamp/BootcampDao.java b/src/main/java/com/roshka/proyectofinal/bootcamp/BootcampDao.java index c07a2c7..634cf91 100644 --- a/src/main/java/com/roshka/proyectofinal/bootcamp/BootcampDao.java +++ b/src/main/java/com/roshka/proyectofinal/bootcamp/BootcampDao.java @@ -15,7 +15,7 @@ public class BootcampDao { 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 (?,?,?,?,?,?,?,?)"); + "insert into bootcamp (id_lenguaje,id_profesor,fecha_inicio,fecha_fin,descripcion,imagen,titulo,activo) values (?,?,?::date,?::date,?,?,?,?)"); ps.setInt(1,b.getId_lenguaje()); ps.setInt(2,b.getId_profesor()); ps.setString(3,b.getFecha_inicio()); @@ -38,7 +38,7 @@ public class BootcampDao { 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=?"); + "update bootcamp set id_lenguaje=?,id_profesor=?,fecha_inicio=?::date,fecha_fin=?::date,descripcion=?,titulo=?,activo=? where id=?"); ps.setInt(1,b.getId_lenguaje()); ps.setInt(2,b.getId_profesor()); ps.setString(3,b.getFecha_inicio()); @@ -50,13 +50,13 @@ public class BootcampDao { status=ps.executeUpdate(); + System.out.println(status); con.close(); }catch(Exception ex){ex.printStackTrace();} return status; } - public static List listar(){ ArrayList list = new ArrayList<>(); String sql = "select a.id, a.fecha_inicio, a.fecha_fin, a.descripcion, a.titulo,\n" + @@ -126,9 +126,8 @@ public class BootcampDao { 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.setId_profesor(rs.getInt("id_profesor")); + b.setId_lenguaje(rs.getInt("id_lenguaje")); b.setImagen(rs.getString("imagen")); } con.close(); diff --git a/src/main/java/com/roshka/proyectofinal/bootcamp/EditServlet.java b/src/main/java/com/roshka/proyectofinal/bootcamp/EditServlet.java index 48035cd..a9acd3a 100644 --- a/src/main/java/com/roshka/proyectofinal/bootcamp/EditServlet.java +++ b/src/main/java/com/roshka/proyectofinal/bootcamp/EditServlet.java @@ -1,24 +1,62 @@ package com.roshka.proyectofinal.bootcamp; +import com.roshka.proyectofinal.entity.Bootcamp; import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.ServletException; import java.io.IOException; - +@WebServlet("/EditServletBootcamp") public class EditServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - response.setContentType("text/html"); String sid=request.getParameter("id"); int id=Integer.parseInt(sid); - request.setAttribute("id", id); + BootcampDao bootcampDao = new BootcampDao(); + Bootcamp bootcamp = bootcampDao.getBootcampById(id); + + request.setAttribute("Bootcamp", bootcamp); RequestDispatcher rd = request.getRequestDispatcher("formulario_bootcamp.jsp"); - rd.forward(request, response); + rd.include(request, response); + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + int id_lenguaje= Integer.parseInt(request.getParameter("id_lenguaje2")); + int id_profesor= Integer.parseInt(request.getParameter("id_profesor2")); + String fecha_inicio=request.getParameter("fecha_inicio2"); + String fecha_fin=request.getParameter("fecha_fin2"); + String descripcion=request.getParameter("descripcion2"); + String imagen=request.getParameter("imagen2"); + String titulo=request.getParameter("titulo2"); + int id = Integer.parseInt(request.getParameter("id")); + String activoStr = request.getParameter("activo2"); + System.out.println(activoStr); + Boolean activo = true; + if ( activoStr == null ) { + activo = false; + }else if (activoStr.equals("on")) { + activo = true; + } + System.out.println(activo); + + + Bootcamp bootcamp =new Bootcamp( id_lenguaje, id_profesor, fecha_inicio, fecha_fin, descripcion, imagen, titulo, activo); + bootcamp.setId(id); + + int status=BootcampDao.update(bootcamp); + + if(status>0){ + response.sendRedirect("formulario_bootcamp.jsp"); + }else{ + System.out.println("Sorry! unable to update record"); + } } + } \ No newline at end of file diff --git a/src/main/java/com/roshka/proyectofinal/bootcamp/SaveServlet.java b/src/main/java/com/roshka/proyectofinal/bootcamp/SaveServlet.java index cd3cf1d..e3ff1fe 100644 --- a/src/main/java/com/roshka/proyectofinal/bootcamp/SaveServlet.java +++ b/src/main/java/com/roshka/proyectofinal/bootcamp/SaveServlet.java @@ -2,6 +2,7 @@ package com.roshka.proyectofinal.bootcamp; import com.roshka.proyectofinal.entity.Bootcamp; import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -9,6 +10,7 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; +@WebServlet("/SaveServletBootcamp") public class SaveServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -23,8 +25,9 @@ public class SaveServlet extends HttpServlet { String imagen=request.getParameter("imagen"); String titulo=request.getParameter("titulo"); String activoStr=request.getParameter("activo"); + System.out.println(activoStr); Boolean activo = false; - if ( activoStr == "on" ) { + if ( activoStr.equals("on") ) { activo = true; } @@ -33,7 +36,7 @@ public class SaveServlet extends HttpServlet { int status= BootcampDao.save(b); if(status>0){ out.print("

Record saved successfully!

"); - request.getRequestDispatcher("index.html").include(request, response); + request.getRequestDispatcher("formulario_bootcamp.jsp").include(request, response); }else{ out.println("Sorry! unable to save record"); } diff --git a/src/main/java/com/roshka/proyectofinal/entity/Lenguaje.java b/src/main/java/com/roshka/proyectofinal/entity/Lenguaje.java index eb6b90a..ffdba97 100644 --- a/src/main/java/com/roshka/proyectofinal/entity/Lenguaje.java +++ b/src/main/java/com/roshka/proyectofinal/entity/Lenguaje.java @@ -8,6 +8,11 @@ public class Lenguaje { } + public Lenguaje(int id, String nombre_lenguaje) { + this.id = id; + this.nombre_lenguaje = nombre_lenguaje; + } + public int getId() { return id; } diff --git a/src/main/java/com/roshka/proyectofinal/lenguaje/EditServlet.java b/src/main/java/com/roshka/proyectofinal/lenguaje/EditServlet.java new file mode 100644 index 0000000..5a28478 --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/lenguaje/EditServlet.java @@ -0,0 +1,46 @@ +package com.roshka.proyectofinal.lenguaje; + +import com.roshka.proyectofinal.entity.Lenguaje; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +@WebServlet("/EditServletLenguaje") +public class EditServlet extends HttpServlet { + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String sid=request.getParameter("id"); + int id=Integer.parseInt(sid); + + LenguajeDao lenguajeDao = new LenguajeDao(); + Lenguaje lenguaje = lenguajeDao.getLenguajeById(id); + + request.setAttribute("Lenguaje", lenguaje); + RequestDispatcher rd = request.getRequestDispatcher("formulario_lenguaje.jsp"); + rd.include(request, response); + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + String nombre_lenguaje=request.getParameter("nombre_lenguaje"); + int id = Integer.parseInt(request.getParameter("id")); + System.out.println(id); + + Lenguaje lenguaje =new Lenguaje(id,nombre_lenguaje); + + int status=LenguajeDao.update(lenguaje); + + if(status>0){ + response.sendRedirect("formulario_lenguaje.jsp"); + }else{ + System.out.println("Sorry! unable to update record"); + } + + } +} diff --git a/src/main/java/com/roshka/proyectofinal/lenguaje/LenguajeDao.java b/src/main/java/com/roshka/proyectofinal/lenguaje/LenguajeDao.java index 4fbcb90..bab4277 100644 --- a/src/main/java/com/roshka/proyectofinal/lenguaje/LenguajeDao.java +++ b/src/main/java/com/roshka/proyectofinal/lenguaje/LenguajeDao.java @@ -66,6 +66,22 @@ public class LenguajeDao { return status; } + public static int update(Lenguaje l){ + int status=0; + try{ + Connection con= DataBase.getConnection(); + PreparedStatement ps=con.prepareStatement( + "update lenguaje set nombre_lenguaje=? where id=?"); + ps.setString(1,l.getNombre_lenguaje()); + ps.setInt(2,l.getId()); + + status=ps.executeUpdate(); + + con.close(); + }catch(Exception ex){ex.printStackTrace();} + + return status; + } public static Lenguaje getLenguajeById(int id){ Lenguaje lenguaje=new Lenguaje(); diff --git a/src/main/java/com/roshka/proyectofinal/profesor/EditServlet.java b/src/main/java/com/roshka/proyectofinal/profesor/EditServlet.java new file mode 100644 index 0000000..9ef6508 --- /dev/null +++ b/src/main/java/com/roshka/proyectofinal/profesor/EditServlet.java @@ -0,0 +1,48 @@ +package com.roshka.proyectofinal.profesor; + +import com.roshka.proyectofinal.entity.Lenguaje; +import com.roshka.proyectofinal.entity.Profesor; +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +@WebServlet("/EditServletProfesor") +public class EditServlet extends HttpServlet { + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + int id = Integer.parseInt(request.getParameter("id")); + + ProfesorDao profesorDao = new ProfesorDao(); + Profesor profesor = profesorDao.getProfesorById(id); + + request.setAttribute("Profesor", profesor); + RequestDispatcher rd = request.getRequestDispatcher("formulario_profesor.jsp"); + rd.include(request, response); + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + int id = Integer.parseInt(request.getParameter("id")); + String nombre = request.getParameter("nombre"); + String apellido = request.getParameter("apellido"); + String email = request.getParameter("correo"); + int nro_cedula = Integer.parseInt(request.getParameter("nro_cedula")); + + Profesor profesor =new Profesor(nro_cedula, nombre, apellido, email); + profesor.setId(id); + + int status=ProfesorDao.update(profesor); + + if(status>0){ + response.sendRedirect("formulario_profesor.jsp"); + }else{ + System.out.println("Sorry! unable to update record"); + } + + } +} diff --git a/src/main/java/com/roshka/proyectofinal/profesor/ProfesorDao.java b/src/main/java/com/roshka/proyectofinal/profesor/ProfesorDao.java index 95dddfa..33a857c 100644 --- a/src/main/java/com/roshka/proyectofinal/profesor/ProfesorDao.java +++ b/src/main/java/com/roshka/proyectofinal/profesor/ProfesorDao.java @@ -1,6 +1,7 @@ package com.roshka.proyectofinal.profesor; import com.roshka.proyectofinal.DataBase; +import com.roshka.proyectofinal.entity.Lenguaje; import com.roshka.proyectofinal.entity.Profesor; import java.sql.Connection; @@ -59,6 +60,26 @@ public class ProfesorDao { return list; } + public static int update(Profesor p){ + int status=0; + try{ + Connection con= DataBase.getConnection(); + PreparedStatement ps=con.prepareStatement( + "update profesor set nombre=?, apellido=?, correo=?, nro_cedula=? where id=?"); + ps.setString(1,p.getNombre()); + ps.setString(2,p.getApellido()); + ps.setString(3,p.getCorreo()); + ps.setInt(4,p.getNro_cedula()); + ps.setInt(5,p.getId()); + + status=ps.executeUpdate(); + + con.close(); + }catch(Exception ex){ex.printStackTrace();} + + return status; + } + public static int delete(int id){ int status=0; try{ diff --git a/src/main/webapp/formulario_bootcamp.jsp b/src/main/webapp/formulario_bootcamp.jsp index f3de4cc..88b6d8d 100644 --- a/src/main/webapp/formulario_bootcamp.jsp +++ b/src/main/webapp/formulario_bootcamp.jsp @@ -1,21 +1,19 @@ +<%@ page import="com.roshka.proyectofinal.entity.Lenguaje, com.roshka.proyectofinal.entity.Bootcamp, com.roshka.proyectofinal.lenguaje.LenguajeDao, com.roshka.proyectofinal.bootcamp.BootcampDao, com.roshka.proyectofinal.entity.Profesor, com.roshka.proyectofinal.profesor.ProfesorDao, java.util.List,java.util.Iterator, java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> - + JSP Page

Crear Bootcamp

- - <%@ page import="com.roshka.proyectofinal.entity.Lenguaje, com.roshka.proyectofinal.entity.Bootcamp, com.roshka.proyectofinal.lenguaje.LenguajeDao, com.roshka.proyectofinal.bootcamp.BootcampDao, com.roshka.proyectofinal.entity.Profesor, com.roshka.proyectofinal.profesor.ProfesorDao, java.util.List,java.util.Iterator" %> - - <% + <% LenguajeDao lenDao = new LenguajeDao(); List listLenguaje = lenDao.listar(); Iterator iter = listLenguaje.iterator(); @@ -26,12 +24,23 @@ Iterator iterProfe = listProfesor.iterator(); Profesor profe = null; %> -
- - + + + + + + + + + + + + - - <% while(iterProfe.hasNext()){ profe = iterProfe.next(); @@ -48,10 +57,13 @@ - <% } %> + <% } %> -
+ +
@@ -87,9 +99,9 @@ <%= boot.getNombre_lenguaje() %> <%= boot.getNombre_profesor() + " " + boot.getApellido_profesor() %> <%= boot.getActivo() %> -
+ > - +
@@ -104,6 +116,64 @@ - + + + <% + LenguajeDao lenDao2 = new LenguajeDao(); + List listLenguaje2 = lenDao2.listar(); + Iterator iter2 = listLenguaje2.iterator(); + Lenguaje len2 = null; + + ProfesorDao profeDao2 = new ProfesorDao(); + List listProfesor2 = profeDao2.listar(); + Iterator iterProfe2 = listProfesor2.iterator(); + Profesor profe2 = null; + Bootcamp bootcampToEdit = (Bootcamp)request.getAttribute("Bootcamp"); + if(bootcampToEdit != null){ + %> +
+ + + + + + + + + + + + > + name="id" id="id" /> + + + + + + + +
+ <% } %> + + \ No newline at end of file diff --git a/src/main/webapp/formulario_lenguaje.jsp b/src/main/webapp/formulario_lenguaje.jsp index 7c58ab0..802cdae 100644 --- a/src/main/webapp/formulario_lenguaje.jsp +++ b/src/main/webapp/formulario_lenguaje.jsp @@ -53,14 +53,14 @@ %> <%= lenguaje.getNombre_lenguaje() %> -
+ >
- > + name="id" id="id" >
@@ -70,6 +70,19 @@ + + <% + Lenguaje lenguajeToEdit = (Lenguaje)request.getAttribute("Lenguaje"); + if(lenguajeToEdit != null){ + %> +
+ + + + +
+ <% } %> + \ No newline at end of file diff --git a/src/main/webapp/formulario_profesor.jsp b/src/main/webapp/formulario_profesor.jsp index 4872868..9fcdab5 100644 --- a/src/main/webapp/formulario_profesor.jsp +++ b/src/main/webapp/formulario_profesor.jsp @@ -72,9 +72,9 @@ <%= profesor.getNro_cedula() %> <%= profesor.getCorreo() %> -
+ > - +
@@ -89,6 +89,25 @@ + + <% + Profesor profesorToEdit = (Profesor)request.getAttribute("Profesor"); + if(profesorToEdit != null){ + %> +
+ + + + + + + + + + +
+ <% } %> + \ No newline at end of file