Create,Delete and Read de bootcamp terminado

parent 09231ba9
......@@ -2,10 +2,11 @@ package com.roshka.proyectofinal.bootcamp;
import com.roshka.proyectofinal.DataBase;
import com.roshka.proyectofinal.entity.Bootcamp;
import com.roshka.proyectofinal.entity.Profesor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BootcampDao {
......@@ -33,4 +34,107 @@ public class BootcampDao {
return status;
}
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;
}
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;
}
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;
}
}
package com.roshka.proyectofinal.bootcamp;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sid=request.getParameter("id");
int id=Integer.parseInt(sid);
System.out.println("Este es el id " + id);
BootcampDao.delete(id);
response.sendRedirect("formulario_bootcamp.jsp");
}
}
package com.roshka.proyectofinal.lenguaje;
package com.roshka.proyectofinal.bootcamp;
import com.roshka.proyectofinal.entity.Lenguaje;
import com.roshka.proyectofinal.entity.Bootcamp;
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 jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.List;
@WebServlet("/ProyectoFinal-Bootcamp/crearBootcamp")
public class ObtenerLenguaje extends HttpServlet {
public class EditServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Lenguaje> len = LenguajeDao.listar();
request.setAttribute("listaLenguaje", len);
RequestDispatcher rqd = request.getRequestDispatcher("./formulario_bootcamp.jsp");
rqd.forward(request, response);
response.setContentType("text/html");
String sid=request.getParameter("id");
int id=Integer.parseInt(sid);
request.setAttribute("id", id);
RequestDispatcher rd = request.getRequestDispatcher("formulario_bootcamp.jsp");
rd.forward(request, response);
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ package com.roshka.proyectofinal.entity;
public class Bootcamp {
private int id, id_lenguaje, id_profesor;
private String fecha_inicio,fecha_fin,descripcion,imagen,titulo;
private String fecha_inicio,fecha_fin,descripcion,imagen,titulo, nombre_profesor, apellido_profesor, nombre_lenguaje;
private boolean activo;
public Bootcamp() {
......@@ -24,6 +24,10 @@ public class Bootcamp {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getId_lenguaje() {
return id_lenguaje;
}
......@@ -81,12 +85,35 @@ public class Bootcamp {
}
public boolean getActivo() {
return activo;
return this.activo;
}
public void setActivo(boolean activo) {
this.activo = activo;
public boolean setActivo(boolean activo) {
return this.activo = activo;
}
public String getNombre_profesor() {
return nombre_profesor;
}
public void setNombre_profesor(String nombre_profesor) {
this.nombre_profesor = nombre_profesor;
}
public String getApellido_profesor() {
return apellido_profesor;
}
public void setApellido_profesor(String apellido_profesor) {
this.apellido_profesor = apellido_profesor;
}
public String getNombre_lenguaje() {
return nombre_lenguaje;
}
public void setNombre_lenguaje(String nombre_lenguaje) {
this.nombre_lenguaje = nombre_lenguaje;
}
}
......@@ -15,6 +15,14 @@ public class Profesor {
this.correo = correo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
......
......@@ -19,6 +19,7 @@ public class LenguajeDao {
Connection con= DataBase.getConnection();
PreparedStatement ps=con.prepareStatement(
"insert into lenguaje (nombre_lenguaje) values (?)");
ps.setString(1,l.getNombre_lenguaje());
status=ps.executeUpdate();
......
package com.roshka.proyectofinal.profesor;
import com.roshka.proyectofinal.DataBase;
import com.roshka.proyectofinal.entity.Bootcamp;
import com.roshka.proyectofinal.entity.Profesor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProfesorDao {
......@@ -26,4 +31,32 @@ public class ProfesorDao {
return status;
}
public static List<Profesor> listar(){
ArrayList<Profesor> list = new ArrayList<>();
String sql = "select * from profesor";
try{
Connection con= DataBase.getConnection();
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Profesor profe = new Profesor();
profe.setId(rs.getInt("id"));
profe.setNombre(rs.getString("nombre"));
profe.setApellido(rs.getString("apellido"));
profe.setNro_cedula(rs.getInt("nro_cedula"));
profe.setCorreo(rs.getString("correo"));
list.add(profe);
}
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return list;
}
}
package com.roshka.proyectofinal.usuario;
import com.roshka.proyectofinal.entity.Usuario;
import com.roshka.proyectofinal.profesor.ProfesorDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SaveServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String nombre=request.getParameter("nombre");
String apellido=request.getParameter("apellido");
String email=request.getParameter("correo");
String contrasena=request.getParameter("contrasena");
Usuario u =new Usuario(nombre, apellido, email, contrasena);
int status= UsuarioDao.save(u);
if(status>0){
out.print("<p>Record saved successfully!</p>");
request.getRequestDispatcher("index.html").include(request, response);
}else{
out.println("Sorry! unable to save record");
}
out.close();
}
}
package com.roshka.proyectofinal.usuario;
import com.roshka.proyectofinal.DataBase;
import com.roshka.proyectofinal.entity.Usuario;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class UsuarioDao {
public static int save(Usuario u){
int status=0;
try{
Connection con= DataBase.getConnection();
PreparedStatement ps=con.prepareStatement(
"insert into usuario (nombre,apellido,contrasena,correo) values (?,?,?,?)");
ps.setString(1,u.getNombre());
ps.setString(2,u.getApellido());
ps.setString(3,u.getContrasena());
ps.setString(4,u.getCorreo());
status=ps.executeUpdate();
con.close();
}catch(Exception ex){ex.printStackTrace();}
return status;
}
}
......@@ -11,7 +11,7 @@ pageEncoding="UTF-8"%>
<div class="container">
<h1>Crear Bootcamp</h1>
<%@ page import="com.roshka.proyectofinal.entity.Lenguaje, com.roshka.proyectofinal.lenguaje.LenguajeDao, java.util.List,java.util.Iterator" %>
<%@ 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();
......@@ -19,7 +19,10 @@ pageEncoding="UTF-8"%>
Iterator<Lenguaje> iter = listLenguaje.iterator();
Lenguaje len = null;
ProfesorDao profeDao = new ProfesorDao();
List<Profesor> listProfesor = profeDao.listar();
Iterator<Profesor> iterProfe = listProfesor.iterator();
Profesor profe = null;
%>
......@@ -35,7 +38,88 @@ pageEncoding="UTF-8"%>
</option>
<% } %>
</select>
<label for="lenguaje">Profesores:</label>
<select name="lenguaje" id="lenguaje">
<% while(iterProfe.hasNext()){
profe = iterProfe.next();
%>
<option value=<%= profe.getId() %> >
<%= profe.getNombre() + " " + profe.getApellido() %>
</option>
<% } %>
</select>
</form>
<%=
Bootcamp editBootcampList = (Bootcamp)request.getAttribute("id");
if (editBootcampList) {
<form action="" method="post">
<label for="titulo">titulo:</label>
<input type="text" name="titulo" id="titulo" value=<%= editBootcampList.getTitulo() %>>
<label for="descripcion">descripcion:</label>
<input type="text" name="descripcion" id="descripcion" value=<%= editBootcampList.getDescripcion() %>>
<label for="fecha_inicio">fecha de inicio:</label>
<input type="text" name="fecha_inicio" id="fecha_inicio" value=<%= editBootcampList.getFecha_inicio() %>>
<label for="fecha_fin">fecha de fin:</label>
<input type="text" name="fecha_fin" id="fecha_fin" value=<%= editBootcampList.getFecha_inicio() %>>
<label for="profesor"> profesor: </label>
<input type="text" name="profesor" id="profesor" value=<%= editBootcampList.getNombre_profesor() %>>
<label for="lenguaje"> lenguaje </label>
<input type="text" name="lenguaje" id="lenguaje">
</form>
}%>
</div>
<div>
<%
BootcampDao bootDao = new BootcampDao();
List<Bootcamp> listBoot = bootDao.listar();
Iterator<Bootcamp> iterBoot = listBoot.iterator();
Bootcamp boot = null;
%>
<table>
<thead>
<tr>
<th>Titulo</th>
<th>Descripcion</th>
<th>fecha de Inicio</th>
<th>Fecha de Fin</th>
<th>Lenguaje</th>
<th>Profesor</th>
<th>Activo</th>
</tr>
</thead>
<tbody>
<% while(iterBoot.hasNext()){
boot = iterBoot.next();
%>
<tr>
<th> <%= boot.getTitulo() %> </th>
<th> <%= boot.getDescripcion() %> </th>
<th> <%= boot.getFecha_inicio() %> </th>
<th> <%= boot.getFecha_fin() %> </th>
<th> <%= boot.getNombre_lenguaje() %> </th>
<th> <%= boot.getNombre_profesor() + " " + boot.getApellido_profesor() %> </th>
<th> <%= boot.getActivo() %> </th>
<th> <form action="/bootcamp/EditServlet">
<input type="hidden" name="id" value=<%= boot.getId() %>>
<input type="submit" value="Editar" > </input>
</form>
</th>
<th>
<form action="DeleteServlet" method="get">
<input type="hidden" name="id" value= <%= boot.getId() %> >
<input type="submit" value="Borrar" > </input>
</form>
</th>
</tr>
<% } %>
</tbody>
</table>
</div>
</body>
</html>
......@@ -24,9 +24,9 @@
</div>
<div class="menu">
<ul>
<li class="link-menu"><a href="/">Home</a></li>
<li class="link-menu"><a href="./home.html">Postulate</a></li>
<li class="link-menu"><a href="/ProyectoFinal-Bootcamp/crearBootcamp">Crear bootcamp</a>
<li class="link-menu"><a href="">Home</a></li>
<li class="link-menu"><a href="formulario.html">Postulate</a></li>
<li class="link-menu"><a href="formulario_bootcamp.jsp">Crear bootcamp</a>
</li>
</ul>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment