mysql 之通過配置文件鏈接數(shù)據(jù)庫
更新時間:2017年08月12日 08:36:12 投稿:lqh
這篇文章主要介紹了mysql 之通過配置文件鏈接數(shù)據(jù)庫的相關(guān)資料,主要是一個單例餓漢式的獲得數(shù)據(jù)庫連接方法工具類的實現(xiàn),需要的朋友可以參考下
mysql 之通過配置文件鏈接數(shù)據(jù)庫
配置文件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
簡單的講一下。配置文件寫了MySQL和Oracle的數(shù)據(jù)庫信息,我的數(shù)據(jù)庫是mysql 所以我把oracle的配置信息注釋掉了。
接下來就是一個單例(餓漢式)的獲得數(shù)據(jù)庫連接方法工具類
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 之通過配置文件鏈接數(shù)據(jù)庫的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Python random模塊制作簡易的四位數(shù)驗證碼
這篇文章主要介紹了Python random模塊制作簡易的四位數(shù)驗證碼,文中給大家提到了python中random模塊的相關(guān)知識,需要的朋友可以參考下2020-02-02
Python報錯之如何解決matplotlib繪圖中文顯示成框框問題
這篇文章主要介紹了Python報錯之如何解決matplotlib繪圖中文顯示成框框問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

