title: Access denied for user 'root'@'localhost'
body: <p>I am trying to fetch records from database. but I am facing this access denied issue. I tried the other solutions mentioned on Stack Overflow like granting privilege to the user.. but none worked. </p>
  
  <p>Code for accessing database : </p>
  
  <pre><code>public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("&lt;html&gt;");
    out.println("&lt;head&gt;&lt;title&gt;Servlet JDBC&lt;/title&gt;&lt;/head&gt;");
    out.println("&lt;body&gt;");
    out.println("&lt;h1&gt;Servlet JDBC&lt;/h1&gt;");
    out.println("&lt;/body&gt;&lt;/html&gt;");  
    // connecting to database
    Connection con = null;  
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees","root","root");
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM employee");
        // displaying records
        while(rs.next()){
        out.print(rs.getObject(1).toString());
        out.print("\t\t\t");
        out.print(rs.getObject(2).toString());
        out.print("&lt;br&gt;");
    }
    } catch (SQLException e) {
        throw new ServletException("Servlet Could not display records.", e);
    } catch (ClassNotFoundException e) {
        throw new ServletException("JDBC Driver not found.", e);
    } finally {
        try {
            if(rs != null) {
                rs.close();
                rs = null;
            }
            if(stmt != null) {
                stmt.close();
                stmt = null;
            }
            if(con != null) {
                con.close();
                con = null;
            }
        } catch (SQLException e) {}
    }
      out.close();
    }
  </code></pre>
  
  <p>Stack trace of the error :</p>
  
  <pre><code>java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
      com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
      com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
      com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4686)
      com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1304)
      com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
      com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
      com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
      com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:834)
      com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      java.lang.reflect.Constructor.newInstance(Unknown Source)
      com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
      com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
      java.sql.DriverManager.getConnection(Unknown Source)
      java.sql.DriverManager.getConnection(Unknown Source)
      WebAppAss.DatabaseAccess.service(DatabaseAccess.java:36)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>What could be the problem in this case. I tried creating a new database but that didn't work too.</p>