我有一张类似这样的地图-
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,但这一次它在建立连接并返回时立即抛出。我在这里做错了什么?