onsir 0 Light Poster

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

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

this my code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    Session session = null;
    try{
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        session =sessionFactory.openSession();
        Object[] options = {"Yes"};
        int n = JOptionPane.showOptionDialog(null, "connect success","test",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);

    }catch(Exception e){
      System.out.println("error connected "+ e.getMessage());
      Object[] options = {"Yes"};
      int n = JOptionPane.showOptionDialog(null, "error connected\n"+ e,"test",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
      }
    }

this file hibernate.cfg.xml i create while running

<?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.url">jdbc:mysql://localhost/hibernatetutorial</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

because i want make connection more flexible using hibernate, who user can connect to other database or server.
thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.