Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts

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>

Read More
    email this       edit
Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database

Trying to connect to a MySQL server from Java? But seeing the error "Unknown database"?

The reason is that the database does not exist in the MySQL server. You might have accidentally misspelled the database name in the JDBC connection url.

See the code below:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sales","username","password");

Here sales is the database name. If you misspelled this or the database sales does not exist in the server, you see this error message.

Read More
    email this       edit
Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


This error message may include the following text also:

"The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."


When we try to connect to a MySQL server "Communications link failure" error may occur for any of the following errors:

i) MySQL server is not running. Check for this and run the sever if it is stopped.

ii) Host name is wrong: If the server is running in the same computer, the correct host name is localhost otherwise it is a valid IP address or computer name.

iii) Wrong port no given : By default MySQL server runs in port 3306. If it is different, check that correct port no is given.


Sample code:

public static Connection getConnection()
{
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
System.out.println("Connection successful");
}
catch(ClassNotFoundException cnfe)
{
System.err.println("MySQL driver not found");
cnfe.printStackTrace();
}
catch(SQLException sqle)
{
System.err.println("Database/Connection error");
sqle.printStackTrace();
}

return connection;
}



Read More
    email this       edit
Published 4/15/2011 by with 2 comments

ClassNotFoundException: com.mysql.jdbc.Driver

This error occurs when the required JDBC driver file is not in the CLASSPATH. Also, if the driver name is not correct ClassNotFoundException occurs. To solve this problem, at first check the driver name; if the driver name is correct, do one of the followings as per requirement.

i) Add an entry for the driver file (mysql-connector-java-5.1.10.jar, for example) in the CLASSPATH variable.

or

ii) If you are not familiar with the CLASSPATH variable, place the mysql-connector-java-5.1.10.jar file in \jre\lib\ext folder in the JDK install directory (for example C:\Program Files\Java\jdk1.6.0_20\jre\lib\ext)

or

iii) If you are doing this in NetBeans or other IDEs, add MySQL JDBC Driver Library in the project.
Read More
    email this       edit