Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. public class ConexionDB {
  2. private Connection cnn;
  3.  
  4. private static ConexionDB instance;
  5.  
  6. private ConexionDB() {
  7.  
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver");
  10. cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/control?useSSL=false",
  11.  
  12. e.printStackTrace();
  13. }
  14. }
  15.  
  16. public static ConexionDB estado() {
  17. if (instance == null) {
  18. instance = new ConexionDB();
  19. }
  20. return instance;
  21. }
  22.  
  23. public Connection getCnn() {
  24. return cnn;
  25. }
  26.  
  27. public void cerraConexion() {
  28. instance = null;
  29. }
  30.  
  31. }
  32.  
  33. String error;
  34. String email = request.getParameter("email");
  35. String password = request.getParameter("password");
  36. HttpSession session = request.getSession();
  37. EmpleadosDAO empleadoDAO = new EmpleadosDAO();
  38. EmpleadosDTO userName = empleadoDAO.login(new EmpleadosDTO(email, password));
  39.  
  40.  
  41. if (userName == null) {
  42. error = "Invalid Email or password";
  43. session.setAttribute("error", error);
  44. response.sendRedirect("index.jsp");
  45. } else {
  46. session.setAttribute("user", userName);
  47. response.sendRedirect("welcome.jsp");
  48. }
  49.  
  50. private final String SQL_LOGIN = "SELECT email from empleados where email=? and password=?";
  51. private ConexionDB con = ConexionDB.estado();
  52.  
  53. public EmpleadosDTO login(EmpleadosDTO c) {
  54.  
  55. java.sql.PreparedStatement ps;
  56. ResultSet res;
  57. EmpleadosDTO l = null;
  58.  
  59. try {
  60.  
  61. ps = con.getCnn().prepareStatement(SQL_LOGIN);
  62.  
  63. ps.setString(1, c.getEmail());
  64. ps.setString(2, c.getPassword());
  65.  
  66. res = ps.executeQuery();
  67. while (res.next()) {
  68. l = new EmpleadosDTO(res.getString(1), res.getString(2));
  69. }
  70. return l;
  71. } catch (SQLException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. } finally {
  75. con.cerraConexion();
  76. }
  77. return l;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement