导入/导出dbunit的xml

前端之家收集整理的这篇文章主要介绍了导入/导出dbunit的xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们如何轻松导入/导出数据库数据,dbunit可以采用以下格式?
<dataset>
   <tablea cola="" colb="" />
   <tableb colc="" cold="" />
</dataset>

我想找到一种从数据库中导出现有数据进行单元测试的方法.

蓝色,这将让您以您想要的格式导出您的数据.
public class DatabaseExportSample {
    public static void main(String[] args) throws Exception {
        // database connection
        Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
        Connection jdbcConnection = DriverManager.getConnection(
                "jdbc:hsqldb:sample","sa","");
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

        // partial database export
        QueryDataSet partialDataSet = new QueryDataSet(connection);
        partialDataSet.addTable("FOO","SELECT * FROM TABLE WHERE COL='VALUE'");
        partialDataSet.addTable("BAR");
        FlatXmlDataSet.write(partialDataSet,new FileOutputStream("partial.xml"));

        // full database export
        IDataSet fullDataSet = connection.createDataSet();
        FlatXmlDataSet.write(fullDataSet,new FileOutputStream("full.xml"));

        // dependent tables database export: export table X and all tables that
        // have a PK which is a FK on X,in the right order for insertion
        String[] depTableNames = 
          TablesDependencyHelper.getAllDependentTables( connection,"X" );
        IDataSet depDataSet = connection.createDataSet( depTableNames );
        FlatXmlDataSet.write(depDataSet,new FileOutputStream("dependents.xml"));
    }
}

猜你在找的XML相关文章