代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
private List<Table> getOriginTableInfos() {
if (this.jdbcTemplate.getDataSource() == null) {
throw new RuntimeException("数据库链接错误");
}
try {
Map<String, Table> tableName2TableMap = new HashMap<>(20);
DatabaseMetaData dbMetaData = this.jdbcTemplate.getDataSource().getConnection().getMetaData();
// 处理表信息
// 此处抛出异常
ResultSet tables = dbMetaData.getTables(
null, null,
ProjectConfig.getTablePrefix() + "_%", new String[]{"TABLE"});
while (tables.next()) {
String tableName = tables.getString("table_name");
String tableDesc = tables.getString("remarks");
tableName2TableMap.put(tableName, new Table(tableName, tableDesc));
}
// 处理列信息
ResultSet columns = dbMetaData.getColumns(
null, null,
ProjectConfig.getTablePrefix() + "%", null);
while (columns.next()) {
String tableName = columns.getString("table_name");
String columnName = columns.getString("column_name");
String typeName = columns.getString("type_name");
String remarks = columns.getString("remarks");
Table table = tableName2TableMap.get(tableName);
if (table == null) {
throw new RuntimeException("Data Wrong When Get Column Info");
}
table.addColumn(new Column(columnName, remarks, typeName));
}
return new ArrayList<>(tableName2TableMap.values());
} catch (Exception e) {
throw new RuntimeException("数据库获取数据错误");
}
}
|
我在代码中注释了抛出异常的代码行,我之前一直以为是我的代码出错了,后来发现是传入的ProjectConfig.getTablePrefix() + "_%"
参数,该参数的实现如下:
1
2
3
4
5
|
public static String getTablePrefix() {
return INSTANCE.tablePrefix;
}
|
因为INSTANCE没有初始化,该字段应该为null,所以抛出来的异常应该为NullException,这个异常被捕获了,变成了一个迷惑我的数据库异常。