954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to make connection to database more dynamically using hibernate.

Hi, all
How to make connection to database more dynamically using hibernate.

Example while my application running, I want to change connection to other server and database name but structure database is still same.

I using jdbc I commonly using code like this

public static void getConnection(){
   try { Class.forName("com.mysql.jdbc.Driver").newInstance() ;
           con=null;
        con=DriverManager.getConnection("jdbc:mysql://"+MainMenu.serverName+
"/" +
                                                      
    MainMenu.dbName + "?user=root&password=root");
            
           } catch (Exception e){
                                  System.out.println(e); 
                                 }
 }

Then if connection to database using file .properties or .xml like this how to make ?

File jdbc.properties

jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost/mydb1
jdbc.driver=com.mysql.jdbc.Driver


or file 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>
	<property
name="connection.url">jdbc:mysql://localhost/mydb1</property>
	<property name="connection.username">root</property>
	<property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="connection.password"></property>
 <property
name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


thanks a lot.

onsir
Light Poster
38 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

Hibernate comes with very good documentation.

but if you don't know how to use properties files, you're not ready for Hibernate.
I'd say you're not ready for JDBC either.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

is true new me learn hibernate. do you know tutorial is best for learn about properties file.
thanks.

onsir
Light Poster
38 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 

the API documentation for the Properties class contains everything you need to know.
If that's not enough, you'd best start looking for another career.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You