Skip to content

Instantly share code, notes, and snippets.

@vigack
Created September 7, 2017 06:19
Show Gist options
  • Save vigack/c5fce1ce2704f50514e84820aa301e4d to your computer and use it in GitHub Desktop.
Save vigack/c5fce1ce2704f50514e84820aa301e4d to your computer and use it in GitHub Desktop.
奇妙BUG记录
private Table getTable(String scheme, String tableName){
Connection connection = null;
Table table = new Table();
table.setTableName(tableName);
try {
List<Column> columns = new ArrayList<>();
connection = getConnection(scheme);
ResultSet resultSet =
connection.getMetaData().getColumns(null, null,tableName,null);
while(resultSet.next()){
Column column = new Column();
String name = resultSet.getString("COLUMN_NAME");
int type = resultSet.getInt("DATA_TYPE");
String typeName = resultSet.getString("TYPE_NAME");
int length = resultSet.getInt("COLUMN_SIZE");
boolean nullable = resultSet.getInt("NULLABLE")==1;
String remark = resultSet.getString("REMARKS");
Object defaultValue = resultSet.getObject("COLUMN_DEF");
column.setName(name);
column.setType(type);
column.setTypeName(typeName);
column.setNullable(nullable);
column.setLength(length);
column.setRemark(remark);
column.setDefaultValue(defaultValue);
columns.add(column);
}
table.setColumns(columns);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(connection);
}
return table;
}
@vigack
Copy link
Author

vigack commented Sep 7, 2017

用jdbc的getMetaData的方式获取列,会获取到重复的列,有时候又获取不到某些字段,很怪异的问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment