jsp專案讀取src目錄下的配置檔案
// app-config.ini 配置檔案存放在專案的 src 目錄下,通過以下幾種方式讀取
private String prop_file = "app-config.ini";
public DbHelper(String dsn) throws IOException, ClassNotFoundException
{
//InputStream in= Thread.currentThread().getContextClassLoader().getResourceAsStream(this.prop_file);
//InputStream in
InputStream in = this.getClass().getClassLoader().getResourceAsStream(this.prop_file);
Properties prop= new Properties();
prop.load(in);
this.driver = prop.getProperty ("driver") ;
this.dsn1
this.dsn2= prop.getProperty ("dsn-db2") ;
this.dsn3= prop.getProperty ("dsn-db3")
;
Class.forName(this.driver);
}
------------------------ app-config.ini 配置檔案的內容:---------------------
driver=com.mysql.jdbc.Driver
dsn-db1=jdbc:mysql://localhost/db1
dsn-db2=jdbc:mysql://localhost/db2?user=root&password=12345&useUnicode=true&characterEncoding=UTF-8
dsn-db3=jdbc:mysql://localhost/db3?user=root&password=12345&useUnicode=true&characterEncoding=UTF-8