天天看點

如何擷取web項目hibernate.cfg.xml配置檔案中的資料

有時候想要擷取hibernate.cfg.xml配置檔案中的資料,網絡上有很多方法,有的很複雜,這裡我介紹一種很簡單的擷取方法。

hibernate.cfg.xml配置檔案中有連接配接資料庫所需的各種資訊,比如這裡要擷取connection.url字段對應的url資料,如下所示:

<property name="connection.url">

jdbc:mysql://218.193.124.5:3306/ctddt

</property>

擷取方法:

String url = HibernateSessionFactory.getConfiguration().getProperties().getProperty("connection.url")

通過以上的方法就可以擷取到connection.url字段對應的url資料。

如果擷取到的url資料中有換行和空格什麼的,隻要把它們都替換成""就可以了,方法改為:

String url=HibernateSessionFactory.getConfiguration().getProperties().getProperty("connection.url").trim().replaceAll("\t","").replaceAll("\n","");

通過上面的方法就可以把擷取到的url資料中的換行和空格去掉。