java連接數據庫(jdbc)調用配置文件


各種語言都有自己所支持的配置文件,后綴名“.properties”結尾的就是其中之一。

在java連接數據庫時,采取讀取配置文件的方式,來獲取數據庫連接。

新建jdbc.properties文件,內容如下:

userName=root
pwd=Lmq2582609/.
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=UTF-8

java的連接代碼:

//Properties 是一個類:
Properties prop=new Properties();
InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");//讀取文件
prop.load(in);//加載文件
userName = prop.getProperty("userName");
pwd = prop.getProperty("pwd");
url = prop.getProperty("url");
driver = prop.getProperty("driver");
Class.forName(driver);// 驅動
conn = DriverManager.getConnection(url, userName, pwd);// 獲取連接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM