DBMetadata
DBMetadata copied to clipboard
示例代码报错
com.github.abel533.database.introspector.DatabaseIntrospector 第331行,原先的columnNamePattern的值是null,但是执行的时候,会抛出该值不可为null的的异常。 我是修改为"%"了,执行正常
你用的什么数据库?
mysql
版本呢?
5.7.16
我用DBS1.0的程序连接数据库试了试没问题,你是通过代码调用遇到的错误?
刚开始做测试的时候,直接参考你给的示例 我的代码:
InputStream in = CodeGenerator.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
throw new RuntimeException("资源文件读取错误,配置文件的位置不正确");
}
SimpleDataSource simpleDataSource = new SimpleDataSource(Dialect.MYSQL,
properties.getProperty("jdbc.url"),
properties.getProperty("jdbc.username"),
properties.getProperty("jdbc.password"));
DBMetadataUtils dbMetadataUtils = new DBMetadataUtils(simpleDataSource);
String catalog = simpleDataSource.getConnection().getCatalog();
DatabaseConfig config = new DatabaseConfig(catalog, "%");
List<IntrospectedTable> list = dbMetadataUtils.introspectTables(config);
for (IntrospectedTable table : list) {
System.out.println(table.getName() + ":");
for (IntrospectedColumn column : table.getAllColumns()) {
System.out.println(column.getName() + " - " +
column.getDefaultValue() + " - " +
column.getJdbcTypeName() + " - " +
column.getJavaProperty() + " - " +
column.getFullyQualifiedJavaType().getFullyQualifiedName() + " - " +
column.getRemarks());
//System.out.println(column.toString());
}
}
异常:
Exception in thread "main" java.sql.SQLException: Column name pattern can not be NULL or empty.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479)
at com.mysql.cj.jdbc.DatabaseMetaData.getColumns(DatabaseMetaData.java:2074)
at com.github.abel533.database.introspector.DatabaseIntrospector.getColumns(DatabaseIntrospector.java:327)
at com.github.abel533.database.introspector.DatabaseIntrospector.introspectTables(DatabaseIntrospector.java:259)
at com.github.abel533.utils.DBMetadataUtils.introspectTables(DBMetadataUtils.java:128)
问题定位到:
protected Map<IntrospectedTable, List<IntrospectedColumn>> getColumns(DatabaseConfig config) throws SQLException {
Map<IntrospectedTable, List<IntrospectedColumn>> answer = new HashMap<IntrospectedTable, List<IntrospectedColumn>>();
ResultSet rs = dbMetadataUtils.getDatabaseMetaData().getColumns(
config.getCatalog(),
config.getSchemaPattern(),
config.getTableNamePattern(),
null);
将getColumns的第四个参数null,改成"%",就可以正常运行了
我找时间试试。
OK,我就先改成%用了