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.

Recommended Answers

All 3 Replies

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.

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

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.

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.