Properties props = new Properties(); try { props.load(props.getClass().getResourceAsStream("hibernate.properties")); }catch(Exception e){ System.out.println("Error loading hibernate properties."); e.printStackTrace(); System.exit(0); } String driver = props.getProperty("hibernate.connection.driver_class"); String connUrl = props.getProperty("hibernate.connection.url"); String username = props.getProperty("hibernate.connection.username"); String password = props.getProperty("hibernate.connection.password"); // In my examples, I use Postgres, but Hibernate // supports virtually every popular dbms out there.Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection(connUrl, username, password); Configuration cfg = new Configuration(); cfg.setProperties( props ); SessionFactory sessions = cfg.buildSessionFactory(); Session session = sessions.openSession(conn); |