0

私はこのような地図を持っています-

ConcurrentHashMap<String, ConcurrentHashMap<String, String>> tableList

このようなデータがあります-

サンプル例-

{table1={DRIVER=oracle.jdbc.driver.OracleDriver, PASSWORD=stage_cs_user, URL=jdbc_url, SUFFIX=xt1, SQL=sql, USER=user}, table2={DRIVER=driver_name, PASSWORD=pass, URL=jdbc_url2, SUFFIX=xt2, SQL=sql2, USER=user}}

上記のマップを繰り返し処理しようとしています-

class Task implements Runnable {

private Connection[] dbConnection = null;
private final ConcurrentHashMap<String, ConcurrentHashMap<String, String>> tableLists;

public Task(ConcurrentHashMap<String, ConcurrentHashMap<String, String>> tableNames) {
    this.tableLists = tableNames;
}

@Override
public void run() {

for (int i = 0; i < tableLists.size(); i++) {

dbConnection[i] = getDBConnection(tableLists.get(i).get("URL"), tableLists.get(i).get("USERNAME"), tableLists.get(i).get("PASSWORD"), tableLists.get(i).get("DRIVER"));
    }
  }
}

そしてそれは私Null Pointer Exceptionに与えていget callます。ここに欠けているものはありますか?はいの場合、どうすれば修正できますか?サイズdbConnectionに応じて0と1を割り当てる必要があるので。tableLists

更新されたコード:-

このような変更を加えた後-

private Connection[] dbConnection = null;


        int j=0;
        for (Map<String, String> map : tableLists.values()) {

            dbConnection[j] = getDBConnection(map.get("URL"), map.get("USER"), map.get("PASSWORD"), map.get("DRIVER"));
            callableStatement[j] = dbConnection[j].prepareCall(map.get("SQL"));

            methods[j] = getRequiredMethods(map.get("SUFFIX"));
            j++;
        }

private Connection getDBConnection(String url, String username, String password, String driver) {

    Connection dbConnection = null;

    try {
        Class.forName(driver);
        dbConnection = DriverManager.getConnection(url, username, password);
    }

    return dbConnection;
}

再びNPEをスローしていますが、今回は接続して戻ってくるとすぐにスローします。私がここで何をしているのですか?

4

2 に答える 2

1

Mapインターフェイスには、Keyインデックスではなく、があります。すべてのエントリを取得するには、

int i=0;
dbConnection = new DbConnection[tableLists.size()];
for (Map<String, String> map : tableLists.values()) {
  dbConnection[i] = getDBConnection(
    map.get("URL"), 
    map.get("USER"), 
    map.get("PASSWORD"), 
    map.get("DRIVER"));
  i++;
}
于 2013-02-13T00:42:30.650 に答える
0

次のような値を取得するべきではありません

tableLists.get(0);
tableLists.get(1);

Mapはインデックスベースのコレクションではないため、キーベースです。次のような値を取得する必要があります

tableLists.get("table1");
tableLists.get("table2");

キーは「table1」と「table2」であり、0と1ではないためです。

編集: したがって、以下のようにソースを変更できます。

int i = 0;
for (String mapkey : tableLists.keySet()) {
    dbConnection[i] = getDBConnection(
            tableLists.get(mapkey).get("URL"), 
            tableLists.get(mapkey).get("USERNAME"),
            tableLists.get(mapkey).get("PASSWORD"),
            tableLists.get(mapkey).get("DRIVER"));
    i++;
}
于 2013-02-13T00:41:20.110 に答える