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各テーブルのそれぞれに 1 つの列名しか表示されません。

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オブジェクトにロードされるキーは 1 つだけです。

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

キー名を別のものに変更するだけです。

それをしたくない場合は、すべての値を単一のキーに連結してから、String#split関数を使用してすべての値を復元できます。

于 2013-02-24T05:59:09.400 に答える