dbunit在获取mysql数据时“No columns found”

    public static void extractTables(String targetDirectory,String[] tableNames)
throws Exception {
IDatabaseConnection connection =new DatabaseConnection( dc.getConnection(), "DB_NAME");
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER,new MySqlMetadataHandler());
for (int i = 0; i < tableNames.length; i++) {
String tableName = tableNames[i];
IDataSet partialDataSet = connection.createDataSet
(new String[] { tableName });
FlatXmlDataSet.write
(partialDataSet, new FileOutputStream
(targetDirectory + "/" + tableName + ".xml"));
}
}

[b]现象:[/b]
运行会出现"No columns found"的错误。DB_NAME为库的名称,一开始认为就是schema.
[b]原因:[/b]
调试后发现,ResultSet resultSet = databaseMetaData.getColumns(
null, schemaName, tableName, "%"); 中,schemaName的值无关痛痒,返回的schema字段为null,在org.dbunit.database.DefaultMetadataHandler#matches中做判断时,由于schema不一致,导致返回col为空
[b]解决办法:[/b]
将"DB_NAME"改为null即可

你可能感兴趣的:(dbunit,exception)