DBMetadata icon indicating copy to clipboard operation
DBMetadata copied to clipboard

示例代码报错

Open kictto opened this issue 8 years ago • 8 comments

com.github.abel533.database.introspector.DatabaseIntrospector 第331行,原先的columnNamePattern的值是null,但是执行的时候,会抛出该值不可为null的的异常。 我是修改为"%"了,执行正常

kictto avatar Apr 27 '17 09:04 kictto

你用的什么数据库?

abel533 avatar Apr 27 '17 13:04 abel533

mysql

kictto avatar Apr 27 '17 13:04 kictto

版本呢?

abel533 avatar Apr 27 '17 13:04 abel533

5.7.16

kictto avatar Apr 27 '17 13:04 kictto

我用DBS1.0的程序连接数据库试了试没问题,你是通过代码调用遇到的错误?

abel533 avatar Apr 28 '17 14:04 abel533

刚开始做测试的时候,直接参考你给的示例 我的代码:

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,改成"%",就可以正常运行了

kictto avatar Apr 29 '17 09:04 kictto

我找时间试试。

abel533 avatar Apr 29 '17 11:04 abel533

OK,我就先改成%用了

kictto avatar Apr 29 '17 11:04 kictto