mercredi 29 juillet 2015

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

Aucun commentaire:

Enregistrer un commentaire