假如配置文件爲DBLINK.properties,在項目的根目錄的conf文件夾下,
配置文件內容如下:
jdbcUrl=jdbc:orcale:thin:@172.16.5.22:1521:XXDB(數據庫名稱) user=xxxx(用戶名) password=xxxx(密碼)
然後在你的數據庫連接類中讀取該配置文件,可以放在加載驅動之前進行讀取,
如下:
static { InputStream in = DBLinkDao.class.getClassLoader().getResourceAsStream("/conf/DBLINK.properties"); (在這行代碼中,DBLinkDao是控制搜索配置文件範圍,如果後面的路徑是絕對路徑,則這個類可以是任意的JAVA類,.class.getClassLoader()是爲了獲得一個類加載器) Properties properties = new Properties(); try{ properties.load(in); (加載文件) } catch (IOException e1) { e1.printStackTrace(); } CONN_STR = properties.getProperty("jdbcUrl"); DB_USER = properties.getProperty("user"); DB_PWD = properties.getProperty("password"); .....(加載驅動代碼) }
之後就正常的數據庫連接操作