Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. CLASSE JAVA CONEXAO
  2. public class ConexaoBD {
  3.     static String url = "jdbc:mysql://localhost:3306/helpdesk";
  4.     static String userdb = "root";
  5.     static String passdb = "";
  6.  
  7.     private ConexaoBD() {}
  8.  
  9.     public static Connection getConnection() throws SQLException {
  10.         try {
  11.             Class.forName("com.mysql.jdbc.Driver");
  12.             return (Connection) DriverManager.getConnection(url, userdb, passdb);
  13.         } catch (ClassNotFoundException e) {
  14.             throw new SQLException(e.getMessage());
  15.         }
  16.     }
  17.    
  18. }
  19.  
  20. SERVLET
  21.  
  22.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  23.     throws ServletException, IOException {
  24.         if ((request.getParameter("acao") != null && request.getParameter("acao").equals("autenticar"))) {
  25.             boolean status;
  26.             try {
  27.                 UsuarioDAO usuario = UsuarioDAO.getInstancia();
  28.                 status = usuario.Autenticar(request.getParameter("login"), request.getParameter("senha"));
  29.                 if (status = true) {
  30.                     response.sendRedirect("index.jsp");
  31.                 } else {
  32.                     response.sendRedirect("login.jsp");
  33.                 }
  34.             } catch (SQLException ex) {
  35.                 Logger.getLogger(UsuarioServlet.class.getName()).log(Level.SEVERE, null, ex);
  36.             }
  37.         }
  38.     }
  39.  
  40. DAO
  41.     public boolean Autenticar(String login, String senha) throws SQLException {
  42.         String sql = "select senha from usuario where login = ?";
  43.         PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
  44.         stmt.setString(1, login);
  45.         ResultSet rs = stmt.executeQuery();
  46.         UsuarioBean usuario = new UsuarioBean();
  47.         while(rs.next()) {
  48.             usuario.setSenha(rs.getString("senha"));
  49.         }
  50.         rs.close();
  51.         stmt.close();
  52.  
  53.         if (senha.equals(usuario.getSenha())) {
  54.             return true;
  55.         } else {
  56.             return false;
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement