Guest User

Untitled

a guest
Nov 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. package modelos;
  2. import static java.lang.System.out;
  3. import java.sql.*;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6.  
  7. public class conexionBD{
  8. public Connection conex;
  9. public static final String CLASS="oracle.jdbc.OracleDriver";
  10. public static final String URL="jdbc:oracle:thin:@localhost:1521:XE";//url de nuestro odbc
  11. public static final String USUARIO="Agenda";
  12. public static final String CLAVE="wkprogra";
  13. public PreparedStatement consulta;
  14. public ResultSet rs;
  15.  
  16. public void conectar(){
  17. try{
  18. Class.forName(CLASS);
  19. this.conex=DriverManager.getConnection(URL,USUARIO,CLAVE);
  20. } catch (SQLException | ClassNotFoundException ex) {
  21. Logger.getLogger(conexionBD.class.getName()).log(Level.SEVERE, null, ex);
  22. }
  23. }
  24.  
  25. public void desconectar() throws SQLException {
  26. this.conex.close();
  27. }
  28.  
  29. public boolean insert(String correo,String contrasena, String nombres, String apellidos) throws SQLException{
  30. this.conectar();
  31. boolean resultado=false;
  32. try{
  33. this.consulta = this.conex.prepareStatement("insert into usuarios(usuario_correo,usuario_contrasena,usuario_nombres,usuario_apellidos,fecha_registro)"
  34. + "values('"+correo+"','"+contrasena+"','"+nombres+"','"+apellidos+"',sysdate)");
  35. resultado =this.consulta.execute();
  36.  
  37. return resultado;
  38. }catch(Exception ex){
  39. out.println(ex.getMessage());
  40. return false;
  41. }
  42. }
  43. }
  44.  
  45. import java.sql.*;
  46.  
  47. public class login {
  48. public boolean auntenticar(String correo,String contrasena) throws SQLException{
  49. boolean log;
  50. conexionBD conexion = new conexionBD();
  51. conexion.conectar();
  52. try{
  53. Statement st = conexion.conex.createStatement();
  54. ResultSet rs;
  55. rs = st.executeQuery("select * from usuarios where usuario_correo='" + correo + "' and usuario_contrasena='" + contrasena + "'");
  56. log = rs.next();
  57. return log;
  58. }catch(Exception ex){
  59. return false;
  60. }
  61. }
  62.  
  63. import modelos.registrar;
  64. import modelos.login;
  65. import java.io.IOException;
  66. import java.io.PrintWriter;
  67. import java.io.Writer;
  68. import javax.servlet.ServletException;
  69. import javax.servlet.http.*;
  70. import java.sql.*;
  71. import java.util.logging.Level;
  72. import java.util.logging.Logger;
  73.  
  74. public class controlServlet extends HttpServlet {
  75. String activarUsuario;
  76. /**
  77. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  78. * methods.
  79. *
  80. * @param request servlet request
  81. * @param response servlet response
  82. * @throws ServletException if a servlet-specific error occurs
  83. * @throws IOException if an I/O error occurs
  84. * @throws java.sql.SQLException
  85. */
  86. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException
  87. {response.setContentType("text/html;charset=UTF-8");
  88. try (PrintWriter out = response.getWriter()) {
  89. /* TODO output your page here. You may use following sample code. */
  90. StringBuffer respuesta= new StringBuffer();
  91. Writer ajaxSalida= response.getWriter();
  92. String correo=request.getParameter("correo");
  93. String contrasena=request.getParameter("contrasena");
  94. login au=new login();
  95. if(au.auntenticar(correo,contrasena)){
  96. response.sendRedirect("home.jsp");
  97. }else{
  98. response.sendRedirect("index.html");
  99. }
  100.  
  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) throws ServletException, IOException {
  115. try{
  116. processRequest(request, response);
  117. }catch(SQLException ex){
  118.  
  119. Logger.getLogger(controlServlet.class.getName()).log(Level.SEVERE,null,ex);
  120. }
  121. }
  122.  
  123. /**
  124. * Handles the HTTP <code>POST</code> method.
  125. *
  126. * @param request servlet request
  127. * @param response servlet response
  128. * @throws ServletException if a servlet-specific error occurs
  129. * @throws IOException if an I/O error occurs
  130. */
  131. @Override
  132. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  133. try{
  134. processRequest(request, response);
  135. }catch(SQLException ex){
  136.  
  137. Logger.getLogger(controlServlet.class.getName()).log(Level.SEVERE,null,ex);
  138. }
  139. }
  140.  
  141. /**
  142. * Returns a short description of the servlet.
  143. *
  144. * @return a String containing servlet description
  145. */
  146. @Override
  147. public String getServletInfo() {
  148. return "Short description";
  149. }// </editor-fold>
  150. }
  151.  
  152. import modelos.conexionBD;
  153. import java.io.IOException;
  154. import java.io.PrintWriter;
  155. import javax.servlet.ServletException;
  156. import javax.servlet.http.HttpServlet;
  157. import javax.servlet.http.HttpServletRequest;
  158. import javax.servlet.http.HttpServletResponse;
  159. import modelos.consultas;
  160. import modelos.registrar;
  161. import java.io.Writer;
  162. import java.sql.*;
  163. import java.sql.SQLException;
  164. import java.util.logging.Level;
  165. import java.util.logging.Logger;
  166.  
  167. public class goToSession extends HttpServlet {
  168.  
  169. /**
  170. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  171. * methods.
  172. *
  173. * @param request servlet request
  174. * @param response servlet response
  175. * @throws ServletException if a servlet-specific error occurs
  176. * @throws IOException if an I/O error occurs
  177. * @throws java.sql.SQLException
  178. */
  179. protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException {
  180. response.setContentType("text/html;charset=UTF-8");
  181. try (PrintWriter out = response.getWriter()) {
  182. /* TODO output your page here. You may use following sample code. */
  183. String correo=request.getParameter("correo2");
  184. String contrasena=request.getParameter("contrasena1");
  185. String nombres=request.getParameter("nombres");
  186. String apellidos=request.getParameter("apellidos");
  187. conexionBD au=new conexionBD();
  188. if(au.insert(correo, contrasena, nombres, apellidos)){
  189. response.sendRedirect("index.html");
  190. }else{
  191. response.getWriter().print("Error");
  192. }
  193. }
  194. }
  195.  
  196. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  197. /**
  198. * Handles the HTTP <code>GET</code> method.
  199. *
  200. * @param request servlet request
  201. * @param response servlet response
  202. * @throws ServletException if a servlet-specific error occurs
  203. * @throws IOException if an I/O error occurs
  204. */
  205. @Override
  206. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  207. try{
  208. processRequest(request, response);
  209. }catch(SQLException ex){
  210.  
  211. Logger.getLogger(goToSession.class.getName()).log(Level.SEVERE,null,ex);
  212. }
  213. }
  214.  
  215. /**
  216. * Handles the HTTP <code>POST</code> method.
  217. *
  218. * @param request servlet request
  219. * @param response servlet response
  220. * @throws ServletException if a servlet-specific error occurs
  221. * @throws IOException if an I/O error occurs
  222. */
  223. @Override
  224. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  225. try{
  226. processRequest(request, response);
  227. }catch(SQLException ex){
  228.  
  229. Logger.getLogger(goToSession.class.getName()).log(Level.SEVERE,null,ex);
  230. }
  231. }
  232.  
  233. /**
  234. * Returns a short description of the servlet.
  235. *
  236. * @return a String containing servlet description
  237. */
  238. @Override
  239. public String getServletInfo() {return "Short description";
  240. }// </editor-fold>
  241.  
  242. }
Add Comment
Please, Sign In to add comment