我正在尝试使用Java jdbc连接从数据库中的相应源系统中检索表名,表中所有行的计数,最大更新时间戳。到目前为止,这就是我想出的。
Socket.io
public Map<String,Long> getGpTableCount() throws SQLException {
List<String> gpTableList = getChunks(); // sourcedb.tablenamename,targetdb.tablename:SSN
Iterator<String> keySetIterator_gpTableList = gpTableList.iterator(); // sourcedb.tablename,targetdb.tablename:SSN
Connection sourcedbCon = (Connection) DbManager.getGpConnection();
PreparedStatement gp_pstmnt = null;
String gpExcpnMsg;
while(keySetIterator_gpTableList.hasNext()) {
String gpTabSchemakey = keySetIterator_gpTableList.next(); // sourcedb.tablename,targetdb.tablename:SSN
String[] tablesnSSNs = gpTabSchemakey.split(","); // tablesnSSNs[0]: sourcedb.tablename, tablesnSSNs[1]: targetdb.tablename:SSN
String[] target = tablesnSSNs[1].split(":"); // target[0]=targetdb.tablename, target[1]=SSN
String[] sourcedbTable = target[0].split("\\."); // sourcedbTable[0] = targetdb, sourcedbTable[1]=table
String gpCountQuery = "select '" + sourcedbTable[1] + "' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from " + tablesnSSNs[0] + " where source_system_name = '" + target[1] + "' group by source_system_name";
System.out.println("\nGP Query: " + gpCountQuery);
try {
gp_pstmnt = sourcedbCon.prepareStatement(gpCountQuery);
ResultSet gpCountRs = gp_pstmnt.executeQuery(); // gpCountRs(1): table, gpCountRs(2): count, gpCountRs(3): source_system_name, gpCountRs(4): max(xx_last_update_tms)
while(gpCountRs.next()) {
System.out.println("sourcedbTable[1]: " + sourcedbTable[1] + " gpCountRs.getString(3): " + gpCountRs.getString(3) + " gpCountRs.getLong(2): " + gpCountRs.getLong(2));
gpCountMap.put(sourcedbTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2)); // (tableName,ssn,12345678)
}
} catch(org.postgresql.util.PSQLException e) {
System.out.println("In SQL Exception block");
gpExcpnMsg = e.getMessage();
gpTabNotFound.put(gpCountQuery, gpExcpnMsg + "\n");
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
System.out.println("In Exception block");
gpExcpnMsg = e.getMessage();
e.printStackTrace();
}
}
System.out.println("GP Connection closed");
gp_pstmnt.close();
sourcedbCon.close();
return gpCountMap;
}
是一个arrayList,它从以下位置接收数组形式的数据:方法gpTableList
我在代码旁边的注释中细分了叙述者中存在的数据格式。这里的问题是,当我运行此方法时,我从表中获取了正确的数据,但它也立即产生了空指针异常。形成的查询,结果和异常消息如下所示:
getChunks()
异常对应的行:
GP Query: select 'xx_table_name' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from sourcedb.xx_table_name where source_system_name = 'ORACLE' group by source_system_name
sourcedbTable[1]: xx_table_name gpCountRs.getString(3): ORACLE gpCountRs.getLong(2): 5181260
In Exception block
Exception Message: null
java.lang.NullPointerException
at com.recordcount.dao.ChunkData.getGpTableCount(ChunkData.java:117)
at com.recordcount.entry.StartCount.main(StartCount.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
这是我的主要课程:
gpCountMap.put(analyticsTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2));
我试图调试代码,但是我无法理解我的错误。 谁能告诉我该如何解决?
答案 0 :(得分:1)
根据您的代码,gpCountMap
为空,您已将其设为全局变量,但未初始化。
为了解决该问题,只需在getGpTableCount
gpCountMap = new HashMap<String,Long>();
或将其添加到main
方法中:
public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
ChunkData chData = new ChunkData();
chData.getGpTableCount();
gpCountMap = new HashMap<String,Long>();
}