1. 程式人生 > >jsp專案讀取src目錄下的配置檔案

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

= DbHelper.class.getClassLoader().getResourceAsStream(this.prop_file); 
        InputStream  in = this.getClass().getClassLoader().getResourceAsStream(this.prop_file); 

        Properties prop= new Properties();

        prop.load(in);


        this.driver  = prop.getProperty ("driver") ; 

        this.dsn1

= prop.getProperty ("dsn-db1") ; 

        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

?user=root&password=12345&useUnicode=true&characterEncoding=UTF-8

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