0

下面是我的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我在每个表中只看到一个列名。

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吗?

4

1 回答 1

3

问题是您在属性文件中多次重复相同的键,因此在加载它时,只有一个键被加载到Properties对象中:

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

只需将您的密钥名称更改为其他名称即可。

If you don't want to do that, you can concatenate all the values in a single key and then use String#split function to recover every value.

于 2013-02-24T05:59:09.400 回答