简体   繁体   中英

Access denied when attempting to connect to mysql from servlet in myeclipse

I'm trying to run a simple servlet/mysql webapp using tomcat server in my eclipse . when I try to connect to the database from a servlet , I get the following error:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create 
PoolableConnectionFactory (Access denied for user ''@'localhost' (using password: YES))

below is the script that I executed:

The servlet:

import java.io.IOException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class EmployeeServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  @Resource(name = "jdbc/testDB")
    DataSource ds;

  public EmployeeServlet() {
    super();
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
      Connection con = ds.getConnection();

      Statement stmt = con.createStatement();
      String query = "select * from Employee";
      ResultSet rs = stmt.executeQuery(query);

      PrintWriter out = response.getWriter();
      response.setContentType("text/html");
      out.print("<center><h1>Employee Details</h1></center>");
      out.print("<html><body>");
      out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
      out.print("<tr><th>Employee ID</th>");
      out.print("<th>Employee Name</th>");
      out.print("<th>Salary</th>");
      out.print("<th>Department</th></tr>");

      while (rs.next()) {
        out.print("<tr>");
        out.print("<td>" + rs.getInt("emp_id") + "</td>");
        out.print("<td>" + rs.getString("emp_name") + "</td>");
        out.print("<td>" + rs.getDouble("salary") + "</td>");
        out.print("<td>" + rs.getString("dept_name") + "</td>");
        out.print("</tr>");
      }
      out.print("</table></body></html>");
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  }
}

content of context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true">
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
  <Resource name="jdbc/testDB" auth="Container"
    type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000" 
    username="root" password="root"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost/mysql">
</Context>

try connecting to mysql from any other tool / from command prompt with all the information you used in your code to connect to the same. Try including the port also in the connection url. Default port is 3306

如果从远程客户端访问数据库,请使用mysql的GRANT查询为您授予访问数据库的权限。

似乎网址中缺少端口号:

jdbc:mysql://localhost:<port>/mysql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM