Hans van der Laan
Hans van der Laan

Reputation: 545

Hibernate Basic Example not working

I'm trying to implement a basic Hibernate example but I can't get it to work.

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

public class SimpleTest {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().
                configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Lecturer lecturer1 = new Lecturer();
        lecturer1.setFirstName("Fatma");
        lecturer1.setLastName("Meawad");

        session.save(lecturer1);
        tx.commit();

        System.out.println
                ("The lecturer " + lecturer1.getFirstName()+ " "
                        + lecturer1.getLastName()+" is successfully added to your database");

    }
}

Everytime I try to run it I get:

Exception in thread "main" org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:mysql//127.0.0.1:3306/sampledb] at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:77) at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) at SimpleTest.main(SimpleTest.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

I just started with Hibernate today but after a whole day trying I can't get an basic example (I tried other examples) to work. What did I forget?

EDIT: My hibernate.cfg.xml

    <?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>

        <!-- ________________ To be Edited _________________ -->

        <property name="connection.url">jdbc:mysql//127.0.0.1:3306/sampledb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>

        <!-- _____________ End of To be Edited ______________ -->


        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>


        <!-- _________ Defining the Mapping Files ___________ -->

        <mapping resource="Lecturer.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Upvotes: 1

Views: 870

Answers (1)

Carson.Yip
Carson.Yip

Reputation: 1

    <property name="hibernate.hbm2ddl.auto">update</property>

add this in you hibernate.cfg.xml,and try it again

Upvotes: 0

Related Questions