I am trying to connect the access db with hibernate, I know that it is not officially supported db from hibernate still for a specific reason I need to connect the access db(.mdb).
While I try to insert, I got an error like "Cannot open connection" I am totally confused.....
Please find the below code,
.................................................................................................................................... hibernate.cfg.xml,
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//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.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property> <!-- <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property> --> <property name="hibernate.connection.url">jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=D:\mdb\store2.mdb</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.connection.password">user</property> <property name="hibernate.connection.pool_size">100</property> <property name="show_sql">true</property> <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <property name="hibernate.dialect">com.custom.dialect.MSAccessDialect</property> <!--<property name="hibernate.hbm2ddl.auto">update</property>--> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> </session-factory> </hibernate-configuration>
.................................................................................................................................... Hibernate Util Class:
package com.web.util;
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static { try { // Create the SessionFactory from hibernate.cfg.xml Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory(); System.out.println("Successfully Created SessionFactory"); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Open a new Session, if this thread has none yet if (s == null) { s = sessionFactory.openSession(); // Store it in the ThreadLocal variable session.set(s); } return s; }
public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); } }
....................................................................................................................................
....................................................................................................................................
Test Run Class: package com.test.run;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
import com.web.model.Contact; import com.web.util.HibernateUtil;
public class FirstExample { public static void main(String[] args) { Session session = null; Transaction transaction = null;
try{ // This step will read hibernate.cfg.xml and prepare hibernate for use session = HibernateUtil.currentSession(); System.out.println(session.toString()); //Create new instance of Contact and set values in it by reading them from form object Contact contact = new Contact(); //contact.setId(2); contact.setFirstName("ss"); contact.setLastName("dd"); contact.setPassword("ff"); session.saveOrUpdate(contact); System.out.println("Done"); }catch(Exception e){ System.out.println(e.getMessage()); }finally{ // Actual contact insertion will happen at this step HibernateUtil.closeSession(); } } } ....................................................................................................................................
Please help me out.
Thanks in advance.
Thanks, Soumya
|