<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 22, comments - 32, trackbacks - 0, articles - 73
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    首先在web工程src目錄下新建一個database.properties 文件
    內容如下:

    user=root
    password=root
    databaseType=com.mysql.jdbc.Driver
    url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&amp;characterEncoding=UTF8&amp;zeroDateTimeBehavior=convertToNull

     這里的內容隨自己的合適而變化,這里不多說了;

    在新建一個讀取.properties文件新類:

    package com.junhai.tamsys.util;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;

    public class DatabaseConfigure {
     private Properties property;
     private FileInputStream inputFile;
     private FileOutputStream outputFile;

     public DatabaseConfigure() {
      property = new Properties();
     }

     public DatabaseConfigure(String filePath) {
      property = new Properties();
      try {
       inputFile = new FileInputStream(filePath);
       property.load(inputFile);
       inputFile.close();
      } catch (FileNotFoundException e) {
       System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }

     /*
      * 重載函數,得到key的值 @param key 取得其值的鍵 @return key的值
      */
     public String getValue(String key) {
      if (property.containsKey(key)) {
       return property.getProperty(key);

      } else
       return "";
     }

     /*
      * 重載函數,得到key的值
      *
      * @param fileName propertys文件的路徑+文件名 @param key 取得其值的鍵 @return key的值
      */
     public String getValue(String fileName, String key) {
      try {
       String value = "";
       inputFile = new FileInputStream(fileName);
       property.load(inputFile);
       inputFile.close();
       if (property.containsKey(key)) {
        value = property.getProperty(key);
        return value;
       } else
        return value;
      } catch (FileNotFoundException e) {
       e.printStackTrace();
       return "";
      } catch (IOException e) {
       e.printStackTrace();
       return "";
      } catch (Exception ex) {
       ex.printStackTrace();
       return "";
      }
     }

     /*
      * 清除properties文件中所有的key和其值
      */
     public void clear() {
      property.clear();
     }

     /*
      * 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替, 當key不存在時,該key的值是value
      * @param key 要存入的鍵 @param value 要存入的值
      */
     public void setValue(String key, String value) {
      property.setProperty(key, value);
     }

     /*
      * 將更改后的文件數據存入指定的文件中,該文件可以事先不存在。 @param fileName 文件路徑+文件名稱 @param
      * description 對該文件的描述
      */
     public void saveFile(String fileName, String description) {
      try {
       outputFile = new FileOutputStream(fileName);
       property.store(outputFile, description);
       outputFile.close();
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException ioe) {
       ioe.printStackTrace();
      }
     }

     public static void main(String[] args) {
      DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");
      System.out.println(test.getValue("user"));
      System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));
      
     }
    }


    這樣就可以通過key得到相應的value了;
    想在這里多說一點是路徑問題,java工程和web 工程讀取.properties路徑是不一樣的,我在這里就花了不少時間。
    JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");這樣讀取就可以了:
    web工程這樣讀取:DatabaseConfigure  dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()
                                                    .getResource("").getPath()+"database.properties");
     
    因為當服務器啟動后工程里面東西會編譯后加到\WEB-INF\classes這個目錄,服務也是從這個目錄下讀取信息的。所以先取到這個路徑,才能正確讀取到database.properties這里邊的內容。


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产免费观看a大片的网站| 精品久久久久国产免费| 久久精品国产精品亚洲人人| 久久亚洲中文无码咪咪爱| 波多野结衣久久高清免费 | 国精无码欧精品亚洲一区| 久久精品无码免费不卡| 亚洲毛片αv无线播放一区| 91成人免费福利网站在线| 亚洲AV永久无码区成人网站| 久久免费观看国产精品| 亚洲综合视频在线| 日韩不卡免费视频| 亚洲丶国产丶欧美一区二区三区| 狠狠久久永久免费观看| 4hu四虎免费影院www| 亚洲VA中文字幕无码毛片| 毛片免费全部播放无码| 亚洲AV成人影视在线观看 | 亚洲色偷偷偷鲁综合| 久久久精品2019免费观看 | 亚洲福利电影一区二区?| 国产日本一线在线观看免费| 亚洲精品无码中文久久字幕| 亚洲精品无码av天堂| 午夜爽爽爽男女免费观看影院| 亚洲成人一级电影| mm1313亚洲精品无码又大又粗| 国产在线观a免费观看| 亚洲一区二区三区91| 免费人成网站在线播放| 久久精品电影免费动漫| 亚洲中文字幕一区精品自拍| 亚洲第一页综合图片自拍| 少妇无码一区二区三区免费| 亚洲老熟女五十路老熟女bbw| 国产乱辈通伦影片在线播放亚洲 | 亚洲heyzo专区无码综合| 亚洲午夜福利精品无码| 国产91免费视频| jizz免费在线观看|