[英]How to export DBUnit result set from query
Someone asked a similar question here: 1 有人在这里问类似的问题: 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. 它将整个数据库转储到XML平面文件中。 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?
现在如何修改它,以便可以提供SQL select语句并导出单个查询的结果?
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. 这不会像我希望的那样创建文件“ partial-dataset.xml”。 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.
当然,当使它生效时,我将用与我的postgresql数据库一起使用的递归联接替换简单查询。
Thanks siegfried 谢谢齐格菲
Your code is OK. 您的代码正常。 This line
FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml"));
这行
FlatXmlDataSet.write(partialDataSet, new FileOutputStream("partial-dataset.xml"));
, does not create XML file, you have to create it manually before . ,不会创建XML文件,您必须在之前手动创建它。 This is all you need.
这就是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.