Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.29 KB | None | 0 0
  1. <%@page import="pe.edu.sise.bean.ErrorBean"%>
  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. <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
  8. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  9. <title>Actualizar Usuarios</title>
  10. </head>
  11. <body>
  12. <%
  13. if (Integer.parseInt(request.getParameter("IDUSUARIO"))!=0 &&
  14. request.getParameter("USUARIO") != null
  15. && request.getParameter("PASSWORD") != null)
  16. {
  17. session.setAttribute("IDUSUARIO", Integer.parseInt(request.getParameter("IDUSUARIO")));
  18. %>
  19. <div class="container">
  20. <form action="ActualizarUsuarioServlet" method="post">
  21. <div class="form-group">
  22. <h2>Actualizar Usuarios</h2>
  23. <jsp:useBean id="error" scope="request" class="pe.edu.sise.bean.ErrorBean"/>
  24. <%--igual
  25. <%
  26. ErrorBean error = new ErrorBean();
  27. %> --%>
  28. <label>IDUSUARIO</label>
  29. <input type="text" name="txtIdusuario" readonly="readonly" value="<%=request.getParameter("IDUSUARIO")%>" class="form-control">
  30. </div>
  31. <div class="form-group">
  32. <label>USUARIO</label>
  33. <input type="text" name="txtUsuario" value="<%=request.getParameter("USUARIO")%>" class="form-control">
  34. </div>
  35. <div class="form-group">
  36. <label>PASSWORD</label>
  37. <input type="text" name="txtPassword" value="<%=request.getParameter("PASSWORD")%>" class="form-control">
  38. </div>
  39. <div class="form-group text-center">
  40. <button type="submit" class="btn btn-primary">Enviar Modificación</button>
  41. <%
  42. if (error.getMsg() != null){
  43. %>
  44. <div class="alert alert-danger" role="alert">
  45. <jsp:getProperty name="error" property="msg" />
  46. <%-- <%= error.getMsg()%> --%>
  47. </div>
  48. <%
  49. }
  50. %>
  51. </div>
  52. </form>
  53. <% } %>
  54. <a href="<%=request.getContextPath()%>/usuarios.jsp">Atrás</a>
  55. </div>
  56. </body>
  57. </html>
  58.  
  59. <%@page import="pe.edu.sise.bean.ErrorBean"%>
  60. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  61. <!DOCTYPE html>
  62. <html>
  63. <head>
  64. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  65. <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
  66. <script src="js/bootstrap.min.js" type="text/javascript"></script>
  67. <title>Eliminar Usuarios</title>
  68. </head>
  69. <body>
  70. <%
  71. if(request.getParameter("USUARIO")!=null &&
  72. request.getParameter("PASSWORD")!=null)
  73. {
  74. session.setAttribute("USUARIO", request.getParameter("USUARIO"));
  75. %>
  76. <div class="container">
  77. <form action="EliminarUsuarioServlet" method="post">
  78. <div class="form-group">
  79. <h2>Eliminar Usuarios</h2>
  80. <jsp:useBean id="error" scope="request" class="pe.edu.sise.bean.ErrorBean" />
  81. <%--igual
  82. <%
  83. ErrorBean error = new ErrorBean();
  84. %> --%>
  85. <label>Usuario</label>
  86. <input type="text" name="txtUsuario" value="<%=request.getParameter("USUARIO")%>" class="form-control">
  87. </div>
  88. <div class="form-group">
  89. <label>Password</label>
  90. <input type="text" name="txtPassword" value="<%=request.getParameter("PASSWORD")%>" class="form-control">
  91. </div>
  92.  
  93. <div class="form-group text-center">
  94. <button type="submit" class="btn btn-primary" >Confirmar Eliminación de Usuario</button>
  95. <%
  96. if (error.getMsg() != null) {
  97. %>
  98. <div class="alert alert-danger" role="alert">
  99. <jsp:getProperty name="error" property="msg" />
  100. <%-- <%= error.getMsg()%> --%>
  101. </div>
  102. <%
  103. }
  104. %>
  105. </div>
  106. </form>
  107. <% } %>
  108. <a href="<%=request.getContextPath()%>/usuarios.jsp">Atrás</a>
  109. </div>
  110. </body>
  111. </html>
  112.  
  113. <%@page import="pe.edu.sise.bean.Usuarios"%>
  114. <%@page import="java.util.List"%>
  115. <%@page import="pe.edu.sise.dao.ConexionBD"%>
  116. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  117. <!DOCTYPE html>
  118. <html>
  119. <head>
  120. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  121. <link rel="stylesheet" href="css/bootstrap.min.css" />
  122. <title>Mantenimiento Usuarios</title>
  123. </head>
  124. <body>
  125. <table class="table table-striped">
  126. <h2>Usuarios:</h2>
  127. <tr>
  128. <th>IdUsuario</th>
  129. <th>Usuario</th>
  130. <th>Password (Contraseña)</th>
  131. </tr>
  132. <%
  133. ConexionBD conexionBD = new ConexionBD();
  134. List<Usuarios> lista = conexionBD.listarUsuarios();
  135. for (Usuarios u : lista) {
  136. %>
  137. <tr>
  138. <td><%= u.getIDUSUARIO()%></td>
  139. <td><%= u.getUSUARIO()%></td>
  140. <td><%= u.getPASSWORD()%></td>
  141. <td><a href="ActualizarUsuarios.jsp?IDUSUARIO=<%= u.getIDUSUARIO()%>&&USUARIO=<%= u.getUSUARIO()%>&&PASSWORD=<%= u.getPASSWORD()%>">Actualizar</a></td>
  142. <td><a href="EliminarUsuarios.jsp?IDUSUARIO=<%= u.getIDUSUARIO()%>&&USUARIO=<%= u.getUSUARIO()%>&&PASSWORD=<%= u.getPASSWORD()%>">Eliminar</a></td>
  143. </tr>
  144. <%
  145. }
  146. %>
  147. </table>
  148. <center><a href="<%=request.getContextPath()%>/menu.jsp">Ir al menú</a></center>
  149. </body>
  150. </html>
  151.  
  152. package pe.edu.sise.servlet;
  153.  
  154. import java.io.IOException;
  155. import java.io.PrintWriter;
  156. import javax.servlet.RequestDispatcher;
  157. import javax.servlet.ServletContext;
  158. import javax.servlet.ServletException;
  159. import javax.servlet.http.HttpServlet;
  160. import javax.servlet.http.HttpServletRequest;
  161. import javax.servlet.http.HttpServletResponse;
  162. import pe.edu.sise.bean.ErrorBean;
  163. import pe.edu.sise.bean.Usuarios;
  164. import pe.edu.sise.dao.ConexionBD;
  165.  
  166. public class ActualizarUsuarioServlet extends HttpServlet {
  167.  
  168. @Override
  169. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  170. throws ServletException, IOException {
  171. //processRequest(request, response);
  172. }
  173.  
  174. /**
  175. * Handles the HTTP <code>POST</code> method.
  176. *
  177. * @param request servlet request
  178. * @param response servlet response
  179. * @throws ServletException if a servlet-specific error occurs
  180. * @throws IOException if an I/O error occurs
  181. */
  182. @Override
  183. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  184. throws ServletException, IOException {
  185. //processRequest(request, response);
  186. ConexionBD conexionBD = new ConexionBD();
  187. Usuarios usuarios = new Usuarios();
  188.  
  189.  
  190. String nombreUsuario = request.getParameter("txtUsuario");
  191. String password = request.getParameter("txtPassword");
  192. int idusuario = Integer.parseInt(request.getParameter("txtIdusuario"));
  193.  
  194. usuarios.setUSUARIO(nombreUsuario);
  195. usuarios.setPASSWORD(password);
  196. usuarios.setIDUSUARIO(idusuario);
  197.  
  198. int resultado = conexionBD.ActualizarUsuarios(usuarios);
  199.  
  200. if (resultado == 1) {
  201. response.sendRedirect(request.getContextPath() + "/usuarios.jsp");
  202. } else {
  203. //Página Error....
  204. }
  205. }
  206.  
  207. public void gestionaError(HttpServletRequest request, HttpServletResponse response, int code,
  208. String msg) throws Exception {
  209. ErrorBean error = new ErrorBean();
  210. error.setCode(1);
  211. error.setMsg("No se pudo actualizar la información");
  212.  
  213. request.setAttribute("error", error);
  214.  
  215. ServletContext ctx = getServletContext();
  216. RequestDispatcher rd = ctx.getRequestDispatcher("/ActualizarUsuarios.jsp");
  217. rd.forward(request, response);
  218. }
  219.  
  220. /**
  221. * Returns a short description of the servlet.
  222. *
  223. * @return a String containing servlet description
  224. */
  225. @Override
  226. public String getServletInfo() {
  227. return "Short description";
  228. }// </editor-fold>
  229. }
  230.  
  231. package pe.edu.sise.servlet;
  232.  
  233. import java.io.IOException;
  234. import java.io.PrintWriter;
  235. import javax.servlet.RequestDispatcher;
  236. import javax.servlet.ServletContext;
  237. import javax.servlet.ServletException;
  238. import javax.servlet.http.HttpServlet;
  239. import javax.servlet.http.HttpServletRequest;
  240. import javax.servlet.http.HttpServletResponse;
  241. import pe.edu.sise.bean.ErrorBean;
  242. import pe.edu.sise.bean.Usuarios;
  243. import pe.edu.sise.dao.ConexionBD;
  244.  
  245. /**
  246. *
  247. * @author FAMARESAM-PC
  248. */
  249. public class EliminarUsuarioServlet extends HttpServlet {
  250.  
  251.  
  252. @Override
  253. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  254. throws ServletException, IOException {
  255. //processRequest(request, response);
  256. }
  257.  
  258. /**
  259. * Handles the HTTP <code>POST</code> method.
  260. *
  261. * @param request servlet request
  262. * @param response servlet response
  263. * @throws ServletException if a servlet-specific error occurs
  264. * @throws IOException if an I/O error occurs
  265. */
  266. @Override
  267. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  268. throws ServletException, IOException {
  269. //processRequest(request, response);
  270. ConexionBD conexionBD = new ConexionBD();
  271. Usuarios usuarios = new Usuarios();
  272.  
  273. int idusu=Integer.parseInt(request.getParameter("txtIdusuario"));
  274.  
  275. usuarios.setIDUSUARIO(idusu);
  276.  
  277. int resultado = conexionBD.EliminarUsuarios(usuarios);
  278.  
  279. if(resultado == 1){
  280. response.sendRedirect(request.getContextPath() + "/usuarios.jsp");
  281. }else{
  282.  
  283. }
  284. }
  285. public void gestionaError(HttpServletRequest request, HttpServletResponse response, int code,
  286. String msg) throws Exception {
  287. ErrorBean error = new ErrorBean();
  288. error.setCode(1);
  289. error.setMsg("No se pudo registrar la información");
  290.  
  291. request.setAttribute("error", error);
  292.  
  293. ServletContext ctx = getServletContext();
  294. RequestDispatcher rd = ctx.getRequestDispatcher("/EliminarUsuarios.jsp");
  295. rd.forward(request, response);
  296. }
  297.  
  298. /**
  299. * Returns a short description of the servlet.
  300. *
  301. * @return a String containing servlet description
  302. */
  303. @Override
  304. public String getServletInfo() {
  305. return "Short description";
  306. }// </editor-fold>
  307.  
  308. }
  309.  
  310. package pe.edu.sise.dao;
  311.  
  312. // Paso 2: Importar el paquete sql
  313. import java.sql.*;
  314. import java.util.ArrayList;
  315. import java.util.List;
  316. import pe.edu.sise.bean.Producto;
  317. import pe.edu.sise.bean.Usuarios;
  318.  
  319.  
  320. public class ConexionBD {
  321.  
  322. Connection con = null;
  323. ResultSet rs = null;
  324.  
  325. public ConexionBD() {
  326. try {
  327. //Paso 1: Registrar el Driver de Conexión de SQL Server
  328. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  329. } catch (Exception ex) {
  330. System.err.println("Exception: " + ex.getMessage());
  331. }
  332. }
  333.  
  334. public boolean loginUsuario(String usuario, String contrasenia) {
  335. boolean usuarioValido = false;
  336. //Paso 3: Conectarse a la Base de Datos
  337. try {
  338. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  339. //Paso 4: ejecuto mi Consulta/Query
  340. String sql = "SELECT USUARIO, PASSWORD FROM TBL_USER WHERE USUARIO=? AND PASSWORD=?";
  341.  
  342. PreparedStatement prst = con.prepareStatement(sql);
  343. prst.setString(1, usuario.trim());
  344. prst.setString(2, contrasenia.trim());
  345. rs = prst.executeQuery();
  346.  
  347. if (rs.next()) {
  348. usuarioValido = true;
  349. }
  350. con.close();
  351. } catch (SQLException ex) {
  352. System.err.println("SQLException: " + ex.getMessage());
  353. }
  354. return usuarioValido;
  355. }
  356.  
  357. public List<Producto> listarProductos() {
  358. List<Producto> productos = new ArrayList<>();
  359. try {
  360. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  361. String sql = "SELECT * FROM TBL_PRODUCTO";
  362. Statement cmd = con.createStatement();
  363. rs = cmd.executeQuery(sql);
  364.  
  365. while (rs.next()) {
  366. Producto p = new Producto();
  367. p.setID(rs.getInt(1));
  368. p.setNOMBRE(rs.getString(2));
  369. p.setCANTIDAD(rs.getInt(3));
  370. p.setPRECIO_UNIT(rs.getDouble(4));
  371. productos.add(p);
  372. }
  373. con.close();
  374. } catch (SQLException ex) {
  375. System.err.println("SQLException: " + ex.getMessage());
  376. }
  377. return productos;
  378. }
  379.  
  380. public int InsertarProducto(Producto objp) {
  381. int resultado = 0;
  382. try {
  383. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  384. String sql = "INSERT INTO tbl_producto(NOMBRE,CANTIDAD,PRECIO_UNIT) VALUES(?,?,?)";
  385. PreparedStatement ps = con.prepareStatement(sql);
  386. ps.setString(1, objp.getNOMBRE());
  387. ps.setInt(2, objp.getCANTIDAD());
  388. ps.setDouble(3, objp.getPRECIO_UNIT());
  389. resultado = ps.executeUpdate();
  390. con.close();
  391. } catch (SQLException e) {
  392. System.err.println("SQLException: " + e.getMessage());
  393. } finally {
  394.  
  395. }
  396. return resultado;
  397. }
  398.  
  399. public int ActualizarProducto(Producto objp) {
  400. int res = 0;
  401. try {
  402. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  403. String sql = "UPDATE tbl_producto SET NOMBRE=?,CANTIDAD=?,PRECIO_UNIT=? WHERE ID=?";
  404. PreparedStatement ps = con.prepareStatement(sql);
  405. ps.setString(1, objp.getNOMBRE());
  406. ps.setInt(2, objp.getCANTIDAD());
  407. ps.setDouble(3, objp.getPRECIO_UNIT());
  408. ps.setInt(4, objp.getID());
  409.  
  410. res = ps.executeUpdate();
  411. con.close();
  412.  
  413. } catch (SQLException e) {
  414. System.err.println("SQLException: " + e.getMessage());
  415. } finally {
  416.  
  417. }
  418. return res;
  419.  
  420. }
  421.  
  422. public int EliminarProducto(Producto objp) {
  423. int res = 0;
  424. try {
  425. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  426. String sql = "DELETE FROM TBL_PRODUCTO WHERE ID=?";
  427. PreparedStatement ps = con.prepareStatement(sql);
  428. ps.setInt(1, objp.getID());
  429.  
  430. res = ps.executeUpdate();
  431. con.close();
  432.  
  433. } catch (SQLException e) {
  434. System.err.println("SQLException: " + e.getMessage());
  435. } finally {
  436.  
  437. }
  438. return res;
  439.  
  440. }
  441.  
  442. public List<Usuarios> listarUsuarios() {
  443. List<Usuarios> usuarios = new ArrayList<>();
  444. try {
  445. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  446. String sql = "SELECT * FROM TBL_USER";
  447. Statement cmd = con.createStatement();
  448. rs = cmd.executeQuery(sql);
  449.  
  450. while (rs.next()) {
  451. Usuarios u = new Usuarios();
  452. u.setIDUSUARIO(rs.getInt(1));
  453. u.setUSUARIO(rs.getString(2));
  454. u.setPASSWORD(rs.getString(3));
  455. usuarios.add(u);
  456. }
  457. con.close();
  458. } catch (SQLException ex) {
  459. System.err.println("SQLException: " + ex.getMessage());
  460. }
  461. return usuarios;
  462. }
  463.  
  464. public int InsertarUsuarios(Usuarios obju) {
  465. int res = 0;
  466. try {
  467. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  468. String sql = "INSERT INTO tbl_user(USUARIO, PASSWORD) VALUES(?,?)";
  469. PreparedStatement ps = con.prepareStatement(sql);
  470. ps.setString(1, obju.getUSUARIO());
  471. ps.setString(2, obju.getPASSWORD());
  472. res = ps.executeUpdate();
  473. con.close();
  474. } catch (Exception e) {
  475. System.err.println("SQLException: " + e.getMessage());
  476. } finally {
  477.  
  478. }
  479. return res;
  480. }
  481.  
  482. public int ActualizarUsuarios(Usuarios obju) {
  483. int res = 0;
  484. try {
  485. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  486. String sql = "UPDATE TBL_USER SET USUARIO=?,PASSWORD=? WHERE IDUSUARIO=?";
  487. PreparedStatement ps = con.prepareStatement(sql);
  488. ps.setString(1, obju.getUSUARIO());
  489. ps.setString(2, obju.getPASSWORD());
  490. ps.setInt(3, obju.getIDUSUARIO());
  491. res = ps.executeUpdate();
  492. con.close();
  493.  
  494. } catch (SQLException e) {
  495. System.err.println("SQLException: " + e.getMessage());
  496. } finally {
  497.  
  498. }
  499. return res;
  500. }
  501.  
  502. public int EliminarUsuarios(Usuarios obju) {
  503. int res = 0;
  504. try {
  505. con = DriverManager.getConnection("jdbc:sqlserver://localhost\.:1433;databaseName=sisebd;user=sa;password=123");
  506. String sql = "DELETE FROM TBL_USER WHERE IDUSUARIO=?";
  507. PreparedStatement ps = con.prepareStatement(sql);
  508. ps.setInt(1, obju.getIDUSUARIO());
  509. res = ps.executeUpdate();
  510. con.close();
  511.  
  512. } catch (SQLException e) {
  513. System.err.println("SQLException: " + e.getMessage());
  514. } finally {
  515.  
  516. }
  517. return res;
  518. }
  519. }
  520.  
  521. package pe.edu.sise.bean;
  522.  
  523. import java.io.Serializable;
  524.  
  525. public class Usuarios implements Serializable{
  526.  
  527. private int IDUSUARIO;
  528. private String USUARIO;
  529. private String PASSWORD;
  530.  
  531. public int getIDUSUARIO() {
  532. return IDUSUARIO;
  533. }
  534.  
  535. public void setIDUSUARIO(int IDUSUARIO) {
  536. this.IDUSUARIO = IDUSUARIO;
  537. }
  538.  
  539.  
  540. public String getUSUARIO() {
  541. return USUARIO;
  542. }
  543.  
  544. public void setUSUARIO(String USUARIO) {
  545. this.USUARIO = USUARIO;
  546. }
  547.  
  548. public String getPASSWORD() {
  549. return PASSWORD;
  550. }
  551.  
  552.  
  553. public void setPASSWORD(String PASSWORD) {
  554. this.PASSWORD = PASSWORD;
  555. }
  556.  
  557. }
  558.  
  559. package pe.edu.sise.bean;
  560.  
  561. import java.io.Serializable;
  562.  
  563. public class ErrorBean implements Serializable{
  564.  
  565. private String msg;
  566. private int code;
  567.  
  568. /**
  569. * @return the msg
  570. */
  571. public String getMsg() {
  572. return msg;
  573. }
  574.  
  575. /**
  576. * @param msg the msg to set
  577. */
  578. public void setMsg(String msg) {
  579. this.msg = msg;
  580. }
  581.  
  582. /**
  583. * @return the code
  584. */
  585. public int getCode() {
  586. return code;
  587. }
  588.  
  589. /**
  590. * @param code the code to set
  591. */
  592. public void setCode(int code) {
  593. this.code = code;
  594. }
  595.  
  596. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement