Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Getting Started with Hibernate Configuring a Project to Use Hibernate

How can i choose which oracle db to connect on run time as per user choice ?

I want to connect to specific database on run time as per user selection , can you please help me with how to do this , i am using oracle database.

3 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Have you tried googling?

Like here, e.g.

http://stackoverflow.com/questions/8491729/java-connecting-to-multiple-databases

Looks like they want to achieve exactly what you want ...

I am creating a java application that connects to multiple databases. A user will be able to select the database they want to connect to from a drop down box.

Thanks Alexander Nikiforov , I saw this but actually i want the same thing to be done by using hibernate . can you help me with how to do this in hibernate . Really appreciate your help.

Thanks Laxmikanta

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Well, I can try but could you tell me the end purpose of this...

What i mean is : if you are using Oracle you are probably trying to create some good App, like WEb app, or what ?

I can try to help you change console App like in 'Hibernate course' you are watching ...

But this app is far away from reality, and again if you need more serious app, I need to know where it goes.

For example If you build Web Application, WEb site back-end with Spring, changing database is quite another problem, and it means that solution for this console App won't help ...

So here is console solution as far as I can, from STack discussion I told you:

I. You create DataSourceUtil class

public final class DataSourceUtil {
    private DataSourceUtil() {

    }
   // NOTE: this "String name" is name of hibernate.cfg.xml with hibernate configuration
    public static SessionFactory getDataSource(String name) {
        final ServiceRegistry registry =
                new StandardServiceRegistryBuilder().configure(name).build();
        return new MetadataSources(registry).buildMetadata().buildSessionFactory();
    }

}

It does exactly what they suggest at forum, creates SEssionFactory with hibernate configuration file as argument

And then in our Main class, or Application following 'Hibernate Basics' course you change buildSEssionFactory class with your new DataSourceUtil.getDataSource class.

public class Application {
    // not final anymore, because can be changed, but static,
    // because we use it in `main`
    private static SessionFactory session;

    public static void main(String[] args) {
        // prompt user for input
        // I put here hibernate.cfg.xml as argument. You can use what user picked
        session = DataSourceUtil.getDataSource("hibernate.cfg.xml");
        // do stuff
        Contact contact1 = new ContactBuilder("John","Doe")
                            .withEmail("john@verizon.net")
                            .withPhone(2222277018L)
                            .build();
        save(contact1);
        System.out.println("This is before the update:");
        getContacts().forEach(System.out::println);
        // close session
        session.close();

        // prompt user for another input
       // I put here hibernate2.cfg.xml as argument. You can use what user picked
        session = DataSourceUtil.getDataSource("hibernate2.cfg.xml");
        // do stuff
        Contact contact2 = new ContactBuilder("John","Doe")
                .withEmail("john@verizon.net")
                .withPhone(2222277018L)
                .build();
        save(contact2);
        System.out.println("This is before the update:");
        getContacts().forEach(System.out::println);
        // close session
        session.close();
    }

    @SuppressWarnings("unchecked")
    private static List<Contact> getContacts() {
        Session sessions = session.openSession();
        Criteria criteria = sessions.createCriteria(Contact.class);
        List<Contact> contacts = criteria.list();
        sessions.close();
        return contacts;
    }

    private static int save(Contact contact){
        Session sessions = session.openSession();
        sessions.beginTransaction();
        int id = (int)sessions.save(contact);
        sessions.getTransaction().commit();
        sessions.close();
        return id;
    }
}

Actually i am trying to create a webapp to practice where i am allowing the user to select which database to connect from a dropdown selection , and then i want the user to run query on that selected database . so want the dynamic runtime db selection , then one you sent above :

you are saying i will keep that different hibernate configuration file and change the file as per user selection , that's a good idea i can try .

thanks if you see any other good idea please let me know

thanks laxmikanta