java - 尝试从 myeclipse 中的 servlet 连接到 mysql 时访问被拒绝

标签 java mysql servlets

我正在尝试在我的 eclipse 中使用 tomcat 服务器运行一个简单的 servlet/mysql webapp。 当我尝试从 servlet 连接到数据库时,出现以下错误:

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

下面是我执行的脚本:

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 {
  }
}

context.xml 文件的内容:

<?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>

最佳答案

尝试从任何其他工具/从命令提示符连接到 mysql,并使用您在代码中使用的所有信息来连接到相同的信息。尝试将端口也包含在连接 URL 中。默认端口为3306

关于java - 尝试从 myeclipse 中的 servlet 连接到 mysql 时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12816157/

相关文章:

java - Camel REST Bean 链接

java - 如果 Optional<String> 不存在或存在但为空字符串,我该怎么办?

java - 如果我们知道实体类的属性名称,是否有办法获取 DynamoDB 属性名称?

mysql - 如何查询这是MySQL?

mysql - 避免第二次询问 mysql root 密码 (bash)

java - Android 在 EditText 中显示来自套接字连接的变量文本

java.sql.SQLException : Parameter index out of range (1 > number of parameters, 即 0)。我收到这个错误

java - Servlet 过滤器 - 不要将过滤器应用于特定过滤器

java - 为什么我无法从 servlet 发送电子邮件并获取 java.security.AccessControlException?

java - 如何寻址我的 java 项目中的文件夹?