將數(shù)據(jù)源的連接配置改為從配置文件讀取
Posted on 2009-10-23 00:14 ZhouFeng 閱讀(3135) 評論(0) 編輯 收藏 所屬分類: 原創(chuàng) 、Web開發(fā)最近因一個項目需要遷移到另一個服務(wù)器上,而遷移的目標(biāo)服務(wù)器是公用的服務(wù)器,需要將原有的數(shù)據(jù)源配置改為從自定義的配置文件讀取,服務(wù)器都是用的Tomcat,以前的項目是在Tomcat里配置數(shù)據(jù)源,然后在程序中獲取數(shù)據(jù)源里的連接,而新的服務(wù)器只提供了一個上傳JSP文件的FTP方式,這種方式是沒辦法修改Tomcat配置文件配置數(shù)據(jù)源的,也就只好修改程序,將數(shù)據(jù)連接的部分改為讀取自定義的配置文件
在此,考慮了幾種方法
方法一:用Spring框架,將數(shù)據(jù)連接配置寫在Spring的配置文件里,用Spring的注入方式生成對象,供程序調(diào)用
方法二:自定義一個xml文件,在數(shù)據(jù)連接寫在xml中,寫一個JavaBean讀取xml
方法三:用Property方式定義配置配置文件,用JavaBean來讀取設(shè)置
因為是一個很小的東東,為此一項而使用Spring框架,似乎有點浪費,用xml來作配置文件,自己寫讀取方法時也不算簡單,最簡單的還是第三種,也不想為這點改動用大刀,簡單的就是最好的:)
首先新建一個讀取配置文件的DBConfig.java(省略了包及getter,setter方法)
然后在WEB-INF目錄下新建配置文件dbconfig.property
在此,考慮了幾種方法
方法一:用Spring框架,將數(shù)據(jù)連接配置寫在Spring的配置文件里,用Spring的注入方式生成對象,供程序調(diào)用
方法二:自定義一個xml文件,在數(shù)據(jù)連接寫在xml中,寫一個JavaBean讀取xml
方法三:用Property方式定義配置配置文件,用JavaBean來讀取設(shè)置
因為是一個很小的東東,為此一項而使用Spring框架,似乎有點浪費,用xml來作配置文件,自己寫讀取方法時也不算簡單,最簡單的還是第三種,也不想為這點改動用大刀,簡單的就是最好的:)
首先新建一個讀取配置文件的DBConfig.java(省略了包及getter,setter方法)
/**
* 這是一個獲取數(shù)據(jù)庫配置的類
* @author ZF
*/
public class DBConfig {
private String url;
private String driver;
private String userName;
private String password;
/**
* @param args
*/
public static void main(String[] args) {
DBConfig conf = new DBConfig();
}
/**
* 在構(gòu)造函數(shù)里讀取配置文件
*/
public DBConfig(){
Properties prop= new Properties();
try
{
//目錄指到根目錄的上級,是為了將配置文件放在WEB-INF目錄下
//如果直接放在根目錄下,則需將配置文件放在classes目錄下
InputStream is = getClass().getResourceAsStream("/../dbconfig.property");
prop.load(is);
if(is!=null){
is.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
this.setDriver(prop.getProperty("Driver"));
this.setUrl(prop.getProperty("URL"));
this.setUserName(prop.getProperty("user"));
this.setPassword(prop.getProperty("password"));
}
// getters()... and setters()...
}
* 這是一個獲取數(shù)據(jù)庫配置的類
* @author ZF
*/
public class DBConfig {
private String url;
private String driver;
private String userName;
private String password;
/**
* @param args
*/
public static void main(String[] args) {
DBConfig conf = new DBConfig();
}
/**
* 在構(gòu)造函數(shù)里讀取配置文件
*/
public DBConfig(){
Properties prop= new Properties();
try
{
//目錄指到根目錄的上級,是為了將配置文件放在WEB-INF目錄下
//如果直接放在根目錄下,則需將配置文件放在classes目錄下
InputStream is = getClass().getResourceAsStream("/../dbconfig.property");
prop.load(is);
if(is!=null){
is.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
this.setDriver(prop.getProperty("Driver"));
this.setUrl(prop.getProperty("URL"));
this.setUserName(prop.getProperty("user"));
this.setPassword(prop.getProperty("password"));
}
// getters()... and setters()...
}
然后在WEB-INF目錄下新建配置文件dbconfig.property
Driver=com.mysql.jdbc.Driver
URL=jdbc:mysql://localhost/tsoa
user=mysqluser
password=mysqlpassword
修改獲取數(shù)據(jù)連接的代碼URL=jdbc:mysql://localhost/tsoa
user=mysqluser
password=mysqlpassword
//獲取數(shù)據(jù)庫連接配置
DBConfig dbconf = new DBConfig();
//根據(jù)連接配置獲取數(shù)據(jù)庫連接
Class.forName(dbconf.getDriver()).newInstance();
conn= DriverManager.getConnection(dbconf.getUrl(),dbconf.getUserName(),dbconf.getPassword());
測試通過,OK,在此記錄一下DBConfig dbconf = new DBConfig();
//根據(jù)連接配置獲取數(shù)據(jù)庫連接
Class.forName(dbconf.getDriver()).newInstance();
conn= DriverManager.getConnection(dbconf.getUrl(),dbconf.getUserName(),dbconf.getPassword());