hibernate.cfg.xml contains info about MySQL database they why I am getting this exception?

Viewed 86689

This is question about java hibernate.

my hibernate.cfg.xml is

 <?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>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/poc</property>
    <property name="hibernate.connection.username">user</property>
    <property name="hibernate.connection.password"/>
    <mapping class="test.person" file="" jar="" package="" resource="person.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Connection to the database is working and I can explore databases and tables,

my sample

person.hbm.xml is

<?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-mapping>
    <class name="test.person" table="person">
        <id name="id" type="int" column="id" >
            <generator class="assigned"/>
        </id>
        <property name="fName">
            <column name="fName" />
        </property>
        <property name="lName">
            <column name="lName"/>
        </property>
        <property name="age">
            <column name="age"/>
        </property>
        <property name="gender">
            <column name="gender"/>
        </property>
    </class>
</hibernate-mapping>

this is my person class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

public class person {

    private int id;
    private String fName;
    private String lName;
    private String gender;
    private int age;

    public person() {
        System.out.println("person");
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public String getfName() {
        return fName;
    }

    public String getGender() {
        return gender;
    }

    public String getlName() {
        return lName;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

}

Here comes my Main class

package test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {


        Session session = null;
        SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();

        person obj = new person();
        obj.setfName("Aqif");
        obj.setlName("Hamid");
        obj.setAge(24);
        obj.setGender("Male");

        session.save(obj);
        session.flush();
        session.close();

    }
}

I am getting this exception,

SEVERE: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:61)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at test.Test.main(Test.java:15)
Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:66)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
    at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at test.Test.main(Test.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.connection.DriverManagerConnectionProvider.configure(DriverManagerConnectionProvider.java:61)
    ... 7 more
Java Result: 1

Please help me resolve these exceptions

I donot know why I am getting this exception

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver

Where as my hibernate.cfg.xml is set for MySQL database


EDIT:

Location of files,

It is located at root of .java files

C:.....NetBeansProjects\test\src

That is src folder of my project.

and java files are in

C:.....NetBeansProjects\test\src\test

I have tried by coping my hibernate.cfg.xml file to some other locations as well.

5 Answers
Related