Guest User

Untitled

a guest
Jan 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package Control;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. /**
  8. *
  9. * @author Zapata
  10. */
  11. public class Conexion2 {
  12.  
  13. private String USERNAME = "root";
  14. private String PASSWORD = "root";
  15. private String HOST = "localhost";
  16. private String PORT = "3306";
  17. private String DATABASE = "registro";
  18. private String CLASSNAME = "com.mysql.jdbc.Driver";
  19. private String URL = "jdbc:mysql://"+HOST+":"+PORT+"/"+DATABASE;
  20. private Connection con;
  21.  
  22. public Conexion2(){
  23. try{
  24. Class.forName(CLASSNAME);
  25. con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  26. } catch (ClassNotFoundException e){
  27. System.err.println("ERROR" + e);
  28. } catch (SQLException e){
  29. System.err.println("ERROR"+ e);
  30. }
  31. }
  32. public Connection getConnection(){
  33. return con;
  34. }
  35. }
  36.  
  37. public class Consultas2 extends Conexion2 {
  38.  
  39. public boolean registrar (String nombre, String contraseña){
  40. PreparedStatement pst = null;
  41. try {
  42. String registrar = "insert into usuarios (nombre, contraseña) values(?, ?)";
  43. pst = getConnection().prepareStatement(registrar);
  44. pst.setString(1, nombre);
  45. pst.setString(2, contraseña);
  46.  
  47. /* if(Nombre == null && Apellido== null && Direccion== null && Imail==null && Contraseña == null && nivel == null){
  48. System.out.println("llena todos los campos");
  49. }*/
  50. if (pst.executeUpdate()== 1){
  51. return true;
  52. }
  53.  
  54. } catch (Exception ex) {
  55. System.err.println("Error" + ex);
  56. }finally{
  57. try {
  58. if (getConnection() != null)getConnection().close();
  59. if (pst != null) pst.close();
  60.  
  61. } catch (Exception e) {
  62. System.err.println("Error" + e);
  63. }
  64. }
  65. return false;
  66. }
  67. }
  68.  
  69. package com.servlet;
  70.  
  71. import Control.Consultas2;
  72. import java.io.IOException;
  73. import java.io.PrintWriter;
  74. import javax.servlet.ServletException;
  75. import javax.servlet.http.HttpServlet;
  76. import javax.servlet.http.HttpServletRequest;
  77. import javax.servlet.http.HttpServletResponse;
  78.  
  79. /**
  80. *
  81. * @author Zapata
  82. */
  83. public class MiServlet extends HttpServlet {
  84.  
  85.  
  86.  
  87. /**Connection
  88. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  89. * methods.
  90. *
  91. * @param request servlet request
  92. * @param response servlet response
  93. * @throws ServletException if a servlet-specific error occurs
  94. * @throws IOException if an I/O error occurs
  95. */
  96. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  97. throws ServletException, IOException {
  98. response.setContentType("text/html;charset=UTF-8");
  99. PrintWriter out = response.getWriter();
  100. /* TODO output your page here. You may use following sample code. */
  101.  
  102. }
  103.  
  104. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  105. /**
  106. * Handles the HTTP <code>GET</code> method.
  107. *
  108. * @param request servlet request
  109. * @param response servlet response
  110. * @throws ServletException if a servlet-specific error occurs
  111. * @throws IOException if an I/O error occurs
  112. */
  113. @Override
  114. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  115. throws ServletException, IOException {
  116. processRequest(request, response);
  117. }
  118.  
  119. /**
  120. * Handles the HTTP <code>POST</code> method.
  121. *
  122. * @param request servlet request
  123. * @param response servlet response
  124. * @throws ServletException if a servlet-specific error occurs
  125. * @throws IOException if an I/O error occurs
  126. */
  127. @Override
  128. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  129. throws ServletException, IOException {
  130.  
  131. response.setContentType("text/html;charset=UTF-8");
  132. PrintWriter out = response.getWriter();
  133.  
  134. String nombre= request.getParameter("nombre");
  135. String contraseña= request.getParameter("contraseña");
  136.  
  137.  
  138. Consultas2 co = new Consultas2();
  139. if(co.registrar(nombre, contraseña)){
  140. out.println("registro exitoso");
  141. }else{
  142. out.println("no se pudo registrar");
  143. }
  144.  
  145. }
Add Comment
Please, Sign In to add comment