how to change database at runtime, i use hibernate and spring.
i have make like this

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>

        <mapping class="com.model.Person" />
        <mapping class="com.model.Group" />
        
    </session-factory>
</hibernate-configuration>

spring.ctx.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    

    <context:property-placeholder location="classpath:jdbc.properties">
    <context:component-scan base-package="com.hbtemplate" />
    <tx:annotation-driven transaction-manager="txManager" />
    
    
   <bean id="dataSource"
   class="org.springframework.jdbc.datasource.DriverManagerDataSource"
   p:driverClassName="${hibernate.connection.driver_class}"
   p:url="${hibernate.connection.url}"
   p:username="${hibernate.connection.username}"
   p:password="${hibernate.connection.password}" />

  
   
   <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
   p:dataSource-ref="dataSource">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.generate_statistics">
                    ${hibernate.show_statistics}
                </prop>
                
                <prop key="hibernate.hbm2ddl.auto">
					${hibernate.hbm2ddl_auto}
				</prop>
            </props>
        </property>
    </bean>
    

        
    <bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" />
              
            </beans>

file hibernate.cfg.xml and file spring.ctx.xml i make in same folder

public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
               
            String PROP_FILE = "jdbc.properties";
            Properties p = new Properties();
          
            p.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
            p.setProperty("hibernate.connection.url","jdbc:mysql://localhost/changeAtRuntime");
            p.setProperty("hibernate.connection.username","root");
            p.setProperty("hibernate.connection.password","");
            p.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLInnoDBDialect");
            p.setProperty("hibernate.hbm2ddl_auto","create");
            p.setProperty("hibernate.show_sql","true");
            p.setProperty("hibernate.show_statistics","true");

            //save properties
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(PROP_FILE);
                    
        } catch (FileNotFoundException ex) {
                  ex.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
               
            }
        }



        //load file properties
        p = new Properties();
            try {
            FileInputStream in = new FileInputStream(PROP_FILE);
            p.load(in);
            in.close();

        } catch (IOException i) {
                i.printStackTrace();
        }


        applicationContext = new ClassPathXmlApplicationContext("spring.ctx.xml");

        }
        });
    }

get error

INFO: Loading properties file from class path resource [jdbc.properties]
Mar 30, 2009 1:28:29 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e0cc23: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,groupDaoHibernate,personDaoHibernate,userService,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,dataSource,sessionFactory,txManager]; root of factory hierarchy
Exception in thread "AWT-EventQueue-0" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist

at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:541)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:515)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)

Thanks for your help

<context:property-placeholder location="classpath:jdbc.properties">

It says, jdbc.properties is not in your classpath. Check whether you got the file in the classpath.

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.