Friday, April 15, 2011

Published 4/15/2011 by with 0 comment

java.sql.SQLException: Access denied for user

The full error message may be :

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password : YES)

or


java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

When we try to connect to a database through JDBC with wrong username or password - we see this error message.

See the sample code below:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","root","mypassword");

In the above code, root is the username and mypassword is the password. If the username root or the password is not correct we see this error message "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password : YES)".

If the password is not provided (blank password) as the code below but the user has password we see the error message "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)"

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","root","");
If you are using JPA with eclipselink/toplink or other, correct the username and password in the persistence.xml file of your project.

persistence.xml may look like below:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="MyPersistenceUnitPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <properties>
      <property name="toplink.jdbc.user" value="root"/>
      <property name="toplink.jdbc.password" value="mypassword"/>
      <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/DatabaseName"/>
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
  </persistence-unit>
</persistence>

    email this       edit

0 comments:

Post a Comment