简体   繁体   中英

How to export DBUnit result set from query

Someone asked a similar question here: 1

This code works:

    IDatabaseConnection connection = getConnection();
    ITableFilter filter = new DatabaseSequenceFilter(connection);
    IDataSet dataset    = new FilteredDataSet(filter, connection.createDataSet());
    FlatXmlDataSet.write(dataset, new FileOutputStream(file));

It dumps the entire database to an XML flat file. Good. We know my username/password/drivername/connection string etc.. are good. Now how do I modify it so I can supply a SQL select statement and export the results of a single query?

I did some google searching and found this code:

    System.out.println("begin partial");
    IDatabaseConnection connection = getConnection();

    QueryDataSet partialDataSet = new QueryDataSet(connection);
    partialDataSet.addTable("gov", "SELECT * FROM GOV_UNIT");
    partialDataSet.addTable("gov_unit");

    FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml"));
    System.out.println("end   partial");

This does not create the file "partial-dataset.xml" like I hoped. It just prints "begin partial" and "end partial".

Can someone help me make this code work? What am I doing wrong? Of course, when get this to work, I'll replace the simple query with a recursive join that works with my postgresql database.

Thanks siegfried

Your code is OK. This line FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml")); , does not create XML file, you have to create it manually before . This is all you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM