user1705985
user1705985

Reputation: 1

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>

Upvotes: 0

Views: 1343

Answers (3)

Bhavik Shah
Bhavik Shah

Reputation: 5183

Use mysql's GRANT query to give you permissions to access the database if you are accessing db from remote client.

Upvotes: 1

Kishore Madina
Kishore Madina

Reputation: 11

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

Upvotes: 1

Rohini Kumar
Rohini Kumar

Reputation: 249

seems port number missing in the url:

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

Upvotes: 0

Related Questions