簡體   English   中英

通過從屬性文件中讀取來填充HashSet

[英]Populate the HashSet by reading it from the property file

下面是我的config.property file

TABLES: table1 table2

#For Table1
table1.url: jdbc:mysql://localhost:3306/garden
table1.user: gardener
table1.password: shavel
table1.driver: jdbc-driver
table1.percentage: 80
table1.column: column1
table1.column: column2
table1.column: column3



#For Table2
table2.url: jdbc:mysql://otherhost:3306/forest
table2.user: forester
table2.password: axe
table2.driver: jdbc-driver
table2.percentage: 20
table2.column: column1
table2.column: column2
table2.column: column3

下面是我的代碼,我試圖在其中讀取以上屬性文件,並通過使用不同的值填充ReadTableConnectionInfo對象,但以某種方式填充HashSet列時,並沒有使用與每個表相對應的所有列名稱來填充。 我在每個columns HashSet每個columns HashSet僅看到一個列名。

private static void readPropertyFile() throws IOException {

    prop.load(Read.class.getClassLoader().getResourceAsStream("config.properties"));

    tableNames = Arrays.asList(prop.getProperty("TABLES").split(" "));

    for (String arg : tableNames) {

        ReadTableConnectionInfo ci = new ReadTableConnectionInfo();

        String url = prop.getProperty(arg + ".url");
        String user = prop.getProperty(arg + ".user");
        String password = prop.getProperty(arg + ".password");
        String driver = prop.getProperty(arg + ".driver");
        String table = prop.getProperty(arg + ".table");
        double percentage = Double.parseDouble(prop.getProperty(arg + ".percentage"));

        String columnPrefix = arg + ".column";
        HashSet<String> columns = new HashSet<String>();
        for (String key : prop.stringPropertyNames()) {
            if (key.startsWith(columnPrefix))
                columns.add(prop.getProperty(key));
        }

        ci.setUrl(url);
        ci.setUser(user);
        ci.setPassword(password);
        ci.setDriver(driver);
        ci.setTableName(table);
        ci.setPercentage(percentage);
        ci.setColumns(columns);

        tableList.put(arg, ci);
    }
}

在填充HashSet列,然后將HashSet列添加到ReadTableConnectionInfo class ,我在這里ReadTableConnectionInfo class什么嗎?

問題是您在屬性文件中重復多次相同的鍵,因此在加載它時,只有一個鍵被加載到Properties對象中:

table1.column: column1
table1.column: column2 //key: table1.column
table1.column: column3 //key: table1.column
//similar for table2

只需將您的密鑰名稱更改為其他名稱即可。

如果您不想這樣做,可以將所有值連接到一個鍵中,然后使用String#split函數恢復每個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM