2011-09-27 89 views
1

我試圖改變hibernate.cfg.xml文件編程 這裏的屬性是我所做的:如何更改cfg.xml文件的屬性?

public class NewHibernateUtil { 

private final SessionFactory sessionFactory; 
String host; 
String database; 
String username; 
String password; 
NewHibernateUtil(String host,String database,String username,String password){ 
    this.host=host; 
    this.database=database; 
    this.username=username; 
    this.password=password; 
    AnnotationConfiguration ac=new AnnotationConfiguration().configure(); 
    ac.setProperty("connection.url", "jdbc:mysql://"+host+"/"+database+""); 
    ac.setProperty("connection.username", username); 
    ac.setProperty("connection.password", password);    
    sessionFactory = ac.buildSessionFactory();   
}  

public SessionFactory getSessionFactory() { 
    return sessionFactory; 
}  
} 

但是這不起作用,它總是使用老樓盤在hibernate.cfg .xml文件。

回答

1

我猜你需要指定屬性的全名進行手動設置時:

ac.setProperty("hibernate.connection.url", "jdbc:mysql://"+host+"/"+database+"");   
ac.setProperty("hibernate.connection.username", username); 
ac.setProperty("hibernate.connection.password", password);  
+0

非常感謝你,問題就迎刃而解了:d – Walllzzz