issue when connecting mysql to java using jdbc

Asked

Viewed 82 times

0

LoginDAO.java:

public class LoginDAO {
 private static String sql = "select * from login where login=? and senha=?";
 private static String url = "jdbc:mysql://localhost:3306/escola";
 private static String username = "root";
 private static String password = "";

public boolean conectar (String login, String senha) {
    try {
        Class.forName("com.mysql.jdbc.driver");
        Connection connection = DriverManager.getConnection(url, username, password);
        PreparedStatement st = connection.prepareStatement(sql);
        st.setString(1, login);
        st.setString(2, senha);
        ResultSet resp = st.executeQuery();
        if (resp.next()) {
            return true;
        }
    } catch (Exception e) {
        throw new RuntimeException ("Erro ao conectar com o banco de dados!");
    }
    return false;
    }
 }

 Login.java:

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    String login = request.getParameter("login");
    String senha = request.getParameter("senha");
    LoginDAO dao = new LoginDAO();
    if (dao.conectar(login, senha)) {
        HttpSession session = request.getSession();
        session.setAttribute("login", login);

        return "index.html";
    } else {
        return "sobre.html";
    }

}

The archive mysql-Connector-java-5.1.48-bin. jar was added to the folder referenced Libraries when I put in the build path.

HTTP Status 500 - Internal Server Error Type Exception Report

Message: Error connecting to database!

Description The server encountered an Unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.Runtimeexception: Error connecting to database! br.com.lerolero.dao.Logindao.conectar(Logindao.java:26) br.com.lerolero.web.Login.execute(Login.java:34) br.com.lerolero.web.Controller.service(Controller.java:37) javax.servlet.http.HttpServlet.service(Httpservlet.java:741) org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53)

1 answer

0


Looking at the statement below there seems to be an error that may be causing the problem in question:

Class.forName("com.mysql.jdbc.driver");

Driver name to be loaded is:

com.mysql.jdbc.Driver

Another tip is not to generalize the capture of exceptions, because makes traceability difficult of the problem.

catch (Exception e) {
    throw new RuntimeException ("Erro ao conectar com o banco de dados!");
}

Prefer to treat separately in this case, making it easy to verify which method an unexpected situation occurred. If you look at the calls from the Try block you will see:

From this, choose to upgrade your catch block with the exceptions that can be released.

  • I managed to fix it. I changed the catch and to "Driver" how you spoke and found out that you were giving a Classnotfoundexception because the file .jar jdbc has to be inside the folder lib in the directory WEB-INF and not in the referenced Libraries, where it was the eclipse itself that added the . jar to the referenced Libraries. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.