Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLASSE JAVA CONEXAO
- public class ConexaoBD {
- static String url = "jdbc:mysql://localhost:3306/helpdesk";
- static String userdb = "root";
- static String passdb = "";
- private ConexaoBD() {}
- public static Connection getConnection() throws SQLException {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- return (Connection) DriverManager.getConnection(url, userdb, passdb);
- } catch (ClassNotFoundException e) {
- throw new SQLException(e.getMessage());
- }
- }
- }
- SERVLET
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- if ((request.getParameter("acao") != null && request.getParameter("acao").equals("autenticar"))) {
- boolean status;
- try {
- UsuarioDAO usuario = UsuarioDAO.getInstancia();
- status = usuario.Autenticar(request.getParameter("login"), request.getParameter("senha"));
- if (status = true) {
- response.sendRedirect("index.jsp");
- } else {
- response.sendRedirect("login.jsp");
- }
- } catch (SQLException ex) {
- Logger.getLogger(UsuarioServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- DAO
- public boolean Autenticar(String login, String senha) throws SQLException {
- String sql = "select senha from usuario where login = ?";
- PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
- stmt.setString(1, login);
- ResultSet rs = stmt.executeQuery();
- UsuarioBean usuario = new UsuarioBean();
- while(rs.next()) {
- usuario.setSenha(rs.getString("senha"));
- }
- rs.close();
- stmt.close();
- if (senha.equals(usuario.getSenha())) {
- return true;
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement