Imagine that you have your database url, driverClassname, username and password in variables with the same name. On your case those variables would be filled from the input page or wherever the user itroduces that data.
And you have a class MyClass class configured with Hibernate annotations on the package com.myapplication.POJO
So, you just create Configuration object and query the database:
Configuration configuration = new Configuration().configure() .setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle8iDialect") .setProperty("hibernate.connection.driver_class", driverClassname) .setProperty("hibernate.connection.url", url) .setProperty("hibernate.connection.username", username) .setProperty("hibernate.connection.password", password) //In next line you just tell Hibernate which classes are you going to query configuration = configuration.addPackage("com.myapplication.POJO"). addAnnotatedClass(MyCustomClass.class); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(ssrb.build()); //We open session with database Session session = factory.openSession(); List<MyCustomClass> result = null; try { //Here we get the list of MyCustomClass objects from database result = session.createQuery("FROM MyCustomClass").list(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); }
IMPORTANT: I think Oracle8iDialect is the most common for Oracle DB. If you are using Oracle 10g or Oracle 9i you should use org.hibernate.dialect.Oracle10gDialect or org.hibernate.dialect.Oracle9iDialect. Complete reference here: http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/dialect/package-summary.html