Thursday, October 11, 2012

Mistake in configuration file.


D:\Java\Custom>java -cp .;mysql-connector-java-5.1.2.jar MainJDBC
java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO
)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3361)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3295)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:909)
        at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3758)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1271)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1965)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:403)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:291)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:283)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at MainJDBC.main(MainJDBC.java:33)
Exception in thread "main" java.lang.NullPointerException
        at MainJDBC.main(MainJDBC.java:56)

D:\Java\Custom>javac MainJDBC.java

D:\Java\Custom>java -cp .;mysql-connector-java-5.1.2.jar MainJDBC
Customer id= 1 quantity= Jayashree


This exception ate my two days of time. Finally there was the configuration file issue therefore it was not even able to take my username and password. Pasting you both the configuration files:

The error prone config:
-----------------------

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>       
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/JPDatabase</property>
        <property name="username">root</property>
        <property name ="password">1234</property>
        <property name="connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>
        <property name ="format_sql">true</property>
        <mapping resource="com/questpond/entity/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration><br/>
<br/>

The corrected config file:
-------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

 <session-factory name="sessionFactory">

  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/JPDatabase</property>
  <property name="hibernate.connection.username">root</property>
  <!-- property name="hibernate.default_schema">ROOT</property-->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>

  <mapping resource="com/jp/entity/Student.hbm.xml"/>
 
 </session-factory>

</hibernate-configuration>

1 comment:

  1. Hi


    I know this but tell me when i am read username,password etc.. from properties file so how to resolve this.

    Any help ?

    ReplyDelete