Guest User

Untitled

a guest
Oct 25th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. <%@page import="java.text.SimpleDateFormat"%>
  2. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  3. <%@page import = "java.sql.*"%>
  4. <%@page import = "java.io.*"%>
  5. <%@page import = "java.util.Date.*" %>
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9.  
  10. <title>Actualizar BD</title>
  11. </head>
  12. <body>
  13. <h1>Actualizacion Tabla Cliente</h1>
  14.  
  15.  
  16.  
  17. <%
  18.  
  19. Connection conexion=null;
  20. //aca llegan los datos desde el formulario
  21. System.out.println("Pasaron por acá");
  22. String ls_rut = request.getParameter("rut");
  23. String ls_nombre = request.getParameter("nombre");
  24. String ls_direccion = request.getParameter("direccion");
  25. String ls_telefono = request.getParameter("telefono");
  26. String ls_correo = request.getParameter("correo");
  27.  
  28. //esto es para recibir la opcion que se eligió(Listar - Eliminar - Crear - Actualizar)
  29. String ls_action = request.getParameter("Action");
  30.  
  31. int control = 0;
  32. //acá se conecta a la bd
  33. String ls_result = "Base de datos actualizada...";
  34. String ls_query = "";
  35. String ls_dbURL = "jdbc:mysql://127.0.0.1:3306/clientefactura";
  36. String ls_usuario = "root";
  37. String ls_password = "";
  38. String ls_dbdriver = "com.mysql.jdbc.Driver";
  39.  
  40. //OPCIONES TABLA CLIENTE
  41. //La opcion crear para que los datos entren en la bd
  42. if(ls_action.equals("Crear")){
  43. System.out.println("Pasó por aqui");
  44. ls_query= "insert into cliente (rut, nombre, direccion, telefono, correo)";
  45. ls_query += "values (";
  46. ls_query += "'"+ ls_rut + "',";
  47. ls_query += "'"+ ls_nombre + "',";
  48. ls_query += "'"+ ls_direccion + "',";
  49. ls_query += "'"+ ls_telefono + "',";
  50. ls_query += "'"+ ls_correo + "')";
  51.  
  52.  
  53. }
  54. //eliminar los datos de la bd
  55. if(ls_action.equals("Eliminar")){
  56. ls_query = "delete from cliente where rut = ";
  57. ls_query += "'" + ls_rut + "'";
  58.  
  59. }
  60.  
  61. //actualizar los datos en la bd
  62. if(ls_action.equals("Actualizar")){
  63.  
  64. ls_query = "UPDATE cliente ";
  65. ls_query += "SET nombre = "+"'"+ ls_nombre + "', direccion = "+"'"+ ls_direccion +"', telefono = "+"'"+ ls_telefono +"', correo = "+"'"+ ls_correo + "'";
  66. ls_query += " where rut = "+"'"+ ls_rut +"'";
  67. }
  68.  
  69. //listar los datos que estan dentro de la bd
  70. if(ls_action.equals("Listar")){
  71.  
  72. ls_query="select rut, nombre , direccion , telefono , correo from cliente";
  73. control = 1;
  74.  
  75. }
  76.  
  77.  
  78.  
  79. //acá pide el driver se requiere para conectarse a mysql
  80.  
  81. Class.forName("com.mysql.jdbc.Driver");
  82.  
  83. Connection dbconn = null;
  84.  
  85.  
  86.  
  87. //acá ingresa a la bd con el usuario y la pass que regularmente son root y las pass vacia
  88. try{
  89.  
  90. Class.forName(ls_dbdriver);
  91.  
  92. dbconn = DriverManager.getConnection(ls_dbURL,ls_usuario,ls_password);
  93.  
  94.  
  95. Statement l_statement = dbconn.createStatement();
  96.  
  97. if (control ==1){
  98. ResultSet rs = l_statement.executeQuery(ls_query);
  99. //acá muestra una tabla entera fea donde salen los datos xd
  100. try {
  101. response.setContentType("text/html");
  102. out.println("<head>"
  103. +"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
  104. +"<link href=\"https://fonts.googleapis.com/css?family=Lato:400,900\" rel=\"stylesheet\">"
  105. +"<link rel=\"stylesheet\" href=\"./css/main.css\">"
  106. +"<title>Formulario</title>"
  107. +"</head>"
  108.  
  109. +"<body background=\"fondo.png\" style=\"background-repeat: no-repeat; background-position: center center;\">"
  110.  
  111. +"<div class=\"container\">"
  112. +"<div class=\"form__top\">"
  113. +"<h2>Datos guardados de <span>Clientes</span></h2>"
  114. +"</div>"
  115. +"<center>"
  116. + "<table width ='311' border='1'>"
  117.  
  118. + "<tr><td width='1665'>Rut</td> <td width='130'>"+ls_rut+"</td></tr>"
  119. + "<tr><td>Nombre</td> <td>"+ls_nombre+"</td></tr><tr><td></td></tr>"
  120. + "<tr><td>Dirección</td> <td>"+ls_direccion+"</td></tr>"
  121. + "<tr><td>Telefono</td> <td>"+ls_telefono+"</td></tr>"
  122. + "<tr><td>Correo</td> <td>"+ls_correo+"</td></tr>"
  123. + "<tr><td colspan='2'></td></tr>"
  124. + "</center></table></body>");
  125. out.close();
  126. }catch (Exception e){}
  127. getServletContext().getRequestDispatcher("/formulario.jsp").forward(request, response);
  128. }
  129.  
  130. //TERMINO TABLA CLIENTE
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. //acá te tira los alert si es que algo salió mal
  140. l_statement.execute(ls_query);
  141.  
  142. } catch (ClassNotFoundException e) {
  143. ls_result = " Error creando el driver!";
  144. ls_result += " <br/>" + e.toString();
  145. } catch (SQLException e) {
  146. ls_result = " Error procesando el SQL!";
  147. ls_result += " <br/>" + e.toString();
  148. }
  149.  
  150. finally {
  151.  
  152. try {
  153. if (dbconn != null) {
  154. dbconn.close();
  155. }
  156. } catch (SQLException e) {
  157. ls_result = "Error al cerrar la conexión.";
  158. ls_result += " <br/>" + e.toString();
  159. }
  160. }
  161. // y al final te muestra lo que se hizo
  162. %>
  163.  
  164.  
  165. La siguiente instrucción fue ejecutada:
  166. <br/><br/>
  167. <%=ls_query%>
  168. <br/><br/>
  169.  
  170. El resultado fue:
  171. <br/><br/>
  172. <%=ls_result%>
  173. <br/><br/>
  174.  
  175.  
  176. <a href="formulariocliente.jsp">Seleccione otra Opción :</a>
  177. <a href ="index.html">Vuelva al inicio aqui</a>
  178. </body>
  179. </html>
Add Comment
Please, Sign In to add comment