Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Pedidos</title>
  8. <link href="css/menu.css" rel="stylesheet" type="text/css"/>
  9. <link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
  10. <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Patua+One">
  11. </head>
  12. <body>
  13. <nav class="navbar navbar-default">
  14. <ul class="nav navbar-nav">
  15. <li><a href="Actividad.jsp">Actividad</a></li>
  16. <li><a class="active" href="Pedidos.jsp">Pedidos</a></li>
  17. </ul>
  18. </nav>
  19.  
  20. <table>
  21. <thead>
  22. <tr>
  23. <th>Id</th>
  24. <th>IdRepartidor</th>
  25. <th>IdPedido</th>
  26. <th>Hora Inicio</th>
  27. <th>Hora Fin</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <c:forEach items="${repartos}" var="reparto">
  32. <tr>
  33. <td>${reparto.id}</td>
  34. <td>${reparto.idRepartidor}</td>
  35. <td>${reparto.idPedido}</td>
  36. <td>${reparto.horaInicio}</td>
  37. <td>${reparto.horaFin}</td>
  38. </tr>
  39. </c:forEach>
  40. </tbody>
  41. </table>
  42. </body>
  43. </html>
  44.  
  45. @WebServlet(name = "ConsultaPedidosServlet", urlPatterns = {"/ConsultaPedidosServlet"})
  46. public class ConsultaPedidosServlet extends HttpServlet {
  47.  
  48. @Override
  49. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  50. throws ServletException, IOException {
  51.  
  52. Consulta cons;
  53. try {
  54. cons = new Consulta("repartos");
  55. ResultSet rs = cons.recuperar("idRepartidor = 11");
  56. List<Repartos> repartos = new ArrayList<>();
  57. while (rs.next()){
  58. int id = rs.getInt("id");
  59. int idRep = rs.getInt("idRepartidor");
  60. int idPed = rs.getInt("idPedido");
  61.  
  62. Repartos rep = new Repartos(id, idRep, idPed);
  63. repartos.add(rep);
  64. }
  65. request.setAttribute("repartos", repartos);
  66. request.getRequestDispatcher("Pedidos.jsp").forward(request, response);
  67. } catch (SQLException | ClassNotFoundException ex) {
  68. Logger.getLogger(ConsultaPedidosServlet.class.getName()).log(Level.SEVERE, null, ex);
  69. }
  70. }
  71.  
  72. public class Consulta {
  73.  
  74. private final String tabla;
  75. private final Connection cnx;
  76.  
  77. public Consulta(String tabla) throws SQLException, ClassNotFoundException{
  78. this.tabla = tabla;
  79. this.cnx = Conexion.obtener();
  80. }
  81.  
  82. public ResultSet recuperar(String condicion) throws SQLException{
  83. ResultSet rs = null;
  84. try{
  85. PreparedStatement consulta = cnx.prepareStatement("SELECT * FROM " + this.tabla + " WHERE " + condicion);
  86. rs = consulta.executeQuery();
  87. }
  88. catch(SQLException ex){
  89. throw new SQLException(ex);
  90. }
  91. return rs;
  92. }
  93. }
  94.  
  95. public class Conexion {
  96.  
  97. private static Connection cnx = null;
  98.  
  99.  
  100. public static Connection obtener() throws SQLException, ClassNotFoundException {
  101. if (cnx == null) {
  102. try {
  103. Class.forName("com.mysql.jdbc.Driver");
  104. cnx = DriverManager.getConnection("jdbc:mysql://localhost/deliverytrackingdb", "root", "root");
  105. } catch (SQLException ex) {
  106. System.out.println(ex.getMessage());
  107. } catch (ClassNotFoundException ex) {
  108. System.out.println(ex.getMessage());
  109. }
  110. }
  111. return cnx;
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement