由于Servlet中的连接导致访问被拒绝

时间:2019-05-27 12:50:35

标签: java-ee

我正在开发一个Java Web应用程序,该应用程序将提取用户输入的数据,并将其比较以进行登录或添加以进行注册。我遇到的问题是,即使我使用了正确的密码并且设置了“ java.sql.SQLException:用户'root'@'localhost'的访问被拒绝(使用密码:是)”错误仍然存​​在也在phpMyAdmin中设置正确的密码。

我已经在phpMyAdmin中检查了root @ localhost的密码,似乎没有一个,但是即使将其更改为正确的密码,它仍然会给我错误。我也查看了特权,但它们都在那里。在检查mysql.user中的Hosts表后,我注意到它为空。可能有问题吗?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // Set response content type
    response.setContentType("text/html");

    String email = request.getParameter("email");
    String psw = request.getParameter("pass");
    PrintWriter out = response.getWriter();
    String docType =
            "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";

    try{
        DataBaseConnection dbc = new DataBaseConnection();
        DataBaseManipulation dbm = new DataBaseManipulation();
        User u2 = new User();
        u2.setFirst_name(email);
        u2.setPassword(psw);

        if(dbm.searchUser(dbc, u2)){
            out.println(docType +
                    "<html>\n" +
                    "<head><title>" + "</title></head>\n" +
                    "<body bgcolor = \"#f0f0f0\">\n" +
                    "<h1 align = \"center\">" + "</h1>\n" +
                    "<ul>\n" +
                    "  <li><b>First Name</b>: "
                    + request.getParameter("first_name") + "\n" +
                    "  <li><b>Last Name</b>: "
                    + request.getParameter("last_name") + "\n" +
                    "</ul>\n" +
                    "</body>" +
                    "</html>"
            );
        }
        else {
            out.println(docType + "Invalid login credentials!");
        }
    } catch (SQLException se) {
        se.printStackTrace();
    }

    response.sendRedirect("home.jsp");
}

我的DataBaseConnection类看起来像这样:

// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/mini_retea";

//  Database credentials
static final String USER = "root";
static final String PASS = "my password";

Connection conn;

public Connection getConn() {
    return conn;
}

public DataBaseConnection() throws SQLException {
    conn = null;
    //Statement stmt = null;

    try {
        //STEP 2: Register JDBC driver
        Class.forName(JDBC_DRIVER);

        //STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

这是我得到的错误:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DataBaseConnection.<init>(DataBaseConnection.java:31) -> this one being where my DriverManager.getConnection is

我只是希望它写入信息并重定向到我的其他jsp文件(不确定是否会打印信息)。

0 个答案:

没有答案