Create hibernate.cfg.xml while program is running

Reply

Join Date: Apr 2007
Posts: 34
Reputation: onsir is an unknown quantity at this point 
Solved Threads: 1
onsir onsir is offline Offline
Light Poster

Create hibernate.cfg.xml while program is running

 
0
  #1
Jun 9th, 2008
how to connet to database using hibernate, because i create hibernate.cfg.xml while is running
i have build simple application for test connection to database using hibernate.
when write application i'm not write hibernate.cfg.xml, but i want create while running.
so when i click button show error like this
[code]
"
"Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Jun 9, 2008 8:59:19 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
error connected /hibernate.cfg.xml not found
"
[\code]

this my code
  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  2. Session session = null;
  3. try{
  4. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  5. session =sessionFactory.openSession();
  6. Object[] options = {"Yes"};
  7. int n = JOptionPane.showOptionDialog(null, "connect success","test",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
  8.  
  9. }catch(Exception e){
  10. System.out.println("error connected "+ e.getMessage());
  11. Object[] options = {"Yes"};
  12. int n = JOptionPane.showOptionDialog(null, "error connected\n"+ e,"test",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
  13. }
  14. }

this file hibernate.cfg.xml i create while running
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
  10. <property name="hibernate.connection.username">root</property>
  11. <property name="hibernate.connection.password"></property>
  12. <property name="hibernate.connection.pool_size">10</property>
  13. <property name="show_sql">true</property>
  14. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  15. <property name="hibernate.hbm2ddl.auto">update</property>
  16. </session-factory>
  17. </hibernate-configuration>

because i want make connection more flexible using hibernate, who user can connect to other database or server.
thx
Last edited by onsir; Jun 9th, 2008 at 6:29 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC