Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConexionDB {
- private Connection cnn;
- private static ConexionDB instance;
- private ConexionDB() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/control?useSSL=false",
- e.printStackTrace();
- }
- }
- public static ConexionDB estado() {
- if (instance == null) {
- instance = new ConexionDB();
- }
- return instance;
- }
- public Connection getCnn() {
- return cnn;
- }
- public void cerraConexion() {
- instance = null;
- }
- }
- String error;
- String email = request.getParameter("email");
- String password = request.getParameter("password");
- HttpSession session = request.getSession();
- EmpleadosDAO empleadoDAO = new EmpleadosDAO();
- EmpleadosDTO userName = empleadoDAO.login(new EmpleadosDTO(email, password));
- if (userName == null) {
- error = "Invalid Email or password";
- session.setAttribute("error", error);
- response.sendRedirect("index.jsp");
- } else {
- session.setAttribute("user", userName);
- response.sendRedirect("welcome.jsp");
- }
- private final String SQL_LOGIN = "SELECT email from empleados where email=? and password=?";
- private ConexionDB con = ConexionDB.estado();
- public EmpleadosDTO login(EmpleadosDTO c) {
- java.sql.PreparedStatement ps;
- ResultSet res;
- EmpleadosDTO l = null;
- try {
- ps = con.getCnn().prepareStatement(SQL_LOGIN);
- ps.setString(1, c.getEmail());
- ps.setString(2, c.getPassword());
- res = ps.executeQuery();
- while (res.next()) {
- l = new EmpleadosDTO(res.getString(1), res.getString(2));
- }
- return l;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- con.cerraConexion();
- }
- return l;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement