3
我必須做一個應用程序,有一個登錄名輸入數據庫作爲管理員,而不必使用配置文件的用戶名和密碼參數,但我不明白,你能幫助我?動態登錄到數據庫休眠
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static void configureHibernateUtil(String user, String pass) {
try {
Configuration cfg = new Configuration();
cfg.configure("/dao/hibernate.cfg.xml"); //hibernate config xml file name
String newUserName = null, newPassword = null;//set them as per your needs
cfg.getProperties().setProperty("hibernate.connection.password", newPassword);
cfg.getProperties().setProperty("hibernate.connection.username", newUserName);
//In next line you just tell Hibernate which classes are you going to query
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(ssrb.build());
} catch (HibernateException he) {
System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
throw new ExceptionInInitializerError(he);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}