mysql 之通過(guò)配置文件鏈接數(shù)據(jù)庫(kù)
mysql 之通過(guò)配置文件鏈接數(shù)據(jù)庫(kù)
配置文件jdbc.properties
##MySQL driver=com.mysql.jdbc.Driver url=jdbc\:mysql\:///ake?useUnicode\=true&characterEncoding\=UTF-8 username=root password=1234 ##Oracle #driver=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@127.0.0.1:1521:orcl #username=scott #password=tiger
簡(jiǎn)單的講一下。配置文件寫了MySQL和Oracle的數(shù)據(jù)庫(kù)信息,我的數(shù)據(jù)庫(kù)是mysql 所以我把oracle的配置信息注釋掉了。
接下來(lái)就是一個(gè)單例(餓漢式)的獲得數(shù)據(jù)庫(kù)連接方法工具類
package Studying.d15; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class ConnUtils { private static Connection con = null; static{ try { Properties p = new Properties(); p.load( new FileInputStream("jdbc.properties") ); String driver = p.getProperty("driver"); String url = p.getProperty("url"); String username = p.getProperty("username"); String password = p.getProperty("password"); System.out.println(url+","+driver); Class.forName(driver); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection(){ return con; } }
以上就是mysql 之通過(guò)配置文件鏈接數(shù)據(jù)庫(kù)的實(shí)例詳解,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Python random模塊制作簡(jiǎn)易的四位數(shù)驗(yàn)證碼
這篇文章主要介紹了Python random模塊制作簡(jiǎn)易的四位數(shù)驗(yàn)證碼,文中給大家提到了python中random模塊的相關(guān)知識(shí),需要的朋友可以參考下2020-02-02解決pyqt5中QToolButton無(wú)法使用的問(wèn)題
今天小編就為大家分享一篇解決pyqt5中QToolButton無(wú)法使用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python報(bào)錯(cuò)之如何解決matplotlib繪圖中文顯示成框框問(wèn)題
這篇文章主要介紹了Python報(bào)錯(cuò)之如何解決matplotlib繪圖中文顯示成框框問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09詳解DeBug Python神級(jí)工具PySnooper
這篇文章主要介紹了詳解DeBug Python神級(jí)工具PySnooper,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07