Mysql.propertiesの使い方
1109 ワード
プロジェクトを別の場所に置くとデータベースが変わります.以下の方法で迅速に変更でき、作成時に便利です.
mysql.properties
相応の用法
mysql.properties
dbUserName=root
dbPassWord=root
dbDriver=com.mysql.jdbc.Driver
dbUrl=jdbc\: mysql\://127.0.0.1\:3306/spdb2?useUnicode\=true&characterEncoding\= utf-8
相応の用法
try
{
// dbinfo.properties
pp = new Properties();
fis=SqlHelper.class.getClassLoader().getResourceAsStream( "mysql.properties");
//fis = new FileInputStream();
pp.load(fis );
url = pp .getProperty("dbUrl");
username = pp .getProperty("dbUserName");
driver = pp .getProperty("dbDriver");
passwd = pp .getProperty("dbPassWord");
Class. forName(driver);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{ fis.close();}
catch(IOException e) {e.printStackTrace();}
fis = null ;//
}