package net.fzjol.common.util;
import Java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.SQLException;
import Java.util.PropertyResourceBundle;
import Java.util.ResourceBundle;
public class DBConn {
private static DBConn conn = null;
private Connection con = null;
// 私有構造方法,使該類不能被實例化
private DBConn() {
}
// 創建該類的一個實例
public synchronized static DBConn getConn() {
if (conn == null) {
conn = new DBConn(); return conn;
}
// 創建數據庫連接
public Connection getConnection() {
ResourceBundle rb = PropertyResourceBundle.getBundle("config");
String dataType = rb.getString("dataType");
String driver = rb.getString(dataType + ".driver");
String url = rb.getString(dataType + ".url");
String user = rb.getString(dataType + ".user");
String passWord = rb.getString(dataType + ".pswd");
try {
Class.forName(driver);// 加載數據庫驅動
con = DriverManager.getConnection(url, user, passWord);// 創建數據庫連接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); return con;
}
====================================
config.propertIEs
#db type(Oracle,sqlserver,MySQL)
dataType = Oracle
#db Oracle
oracle.driver = oracle.jdbc.driver.OracleDriver
oracle.url = jdbc:Oracle:thin:@172.0.0.1:1521:ORCL
Oracle.user = scott
Oracle.pswd = dbmaster
#db sqlserver
sqlserver.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver
sqlserver.url = jdbc:microsoft:sqlserver://172.0.0.1:1433; DatabaseName = test
sqlserver.user = sa
sqlserver.pswd = dbmaster
#db MySQL
mysql.driver = com.MySQL.jdbc.Driver
mysql.url = jdbc:MySQL://localhost:3306/test
MySQL.user = root
MySQL.pswd = dbmaster