I am trying to make a simple project work with hibernate under Eclipse.
I have all the required libraries, a test database in MySQL, and this
directory structure:
ProjectName folder
│ .project
│ .classpath
│ build.xml
│
├───core
│ ├───demo
│ │ Test.java
│ │
│ │
│ ├───hibernate
│ │ date.hbm.xml
│ │
│ ├───util
│ │ HibernateUtil.java
│ │
│ └───dao
│ DateDAO.java
│
├───bin
│ │ date.hbm.xml
│ │ hibernate.cfg.xml
│ │
│ ├───hibernate
│ │ date.hbm.xml
│ │
│ ├───util
│ │ HibernateUtil.class
│ │
│ │
│ ├───demo
│ │ Test.class
│ │
│ └───dao
│ DateDAO.class
│
├───.settings
│ org.eclipse.jdt.core.prefs
│
└───lib
mysql-connector-java-5.1.7-bin.jar
ant-1.6.5.jar
ant-antlr-1.6.5.jar
ant-junit-1.6.5.jar
ant-launcher-1.6.5.jar
antlr-2.7.6.jar
ant-swing-1.6.5.jar
asm.jar
asm-attrs.jar
bsh-2.0b1.jar
c3p0-0.9.1.jar
cglib-2.1.3.jar
checkstyle-all.jar
cleanimports.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
concurrent-1.3.2.jar
connector.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
freemarker.jar
hibernate3.jar
hibernate-tools.jar
jaas.jar
jacc-1_0-fr.jar
javaee.jar
javassist.jar
jaxen-1.1-beta-7.jar
jboss-cache.jar
jboss-common.jar
jboss-jmx.jar
jboss-system.jar
jdbc2_0-stdext.jar
jgroups-2.2.8.jar
jta.jar
jtidy-r8-20060801.jar
junit-4.1.jar
log4j-1.2.11.jar
mysql.jar
mysql-connector-java-5.0.8-bin.jar
oscache-2.1.jar
proxool-0.8.3.jar
swarmcache-1.0rc2.jar
syndiag2.jar
versioncheck.jar
xerces-2.6.2.jar
xml-apis.jar
DateDAO respects the Beans construction.
This is hibernate.cfg.xml
Code:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernatetest</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">3</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- the current unit of work is bound to the current Java thread that executes our application -->
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="date.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
System.out.println("read data: transaction started");
DateDAO valN = new DateDAO();
valN.setId(2);
valN.setVal("val 2");
session.save(valN);
System.out.println("new value added");
session.getTransaction().commit();
System.out.println(" transaction committed");
The error that it throws is at follows:
SessionFactory was created
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection.
I did something wrong?