Hi all i'm working using dbunit. I'm trying to export datset of db into a xml file.
import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import java.io.FileOutputStream;
public class DatabaseExport
{
public static void main(String[] args) throws Exception
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection jdbcConnection = DriverManager.getConnection("jdbc:sqlserver://172.26.37.113:1433;DatabaseName=def_config","sqladmin_","halloween$2004");
IDatabaseConnection connection = new DatabaseConnection( jdbcConnection );
// full database export
IDataSet fullDataSet = connection.createDataSet();
FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));
}
}
But it gives an error like this:
Exception in thread "main" org.dbunit.dataset.DataSetException: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'user'.
at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:323)
at org.dbunit.database.DatabaseTableIterator.getTable(DatabaseTableIterator.java:89)
at org.dbunit.dataset.stream.DataSetProducerAdapter.produce(DataSetProducerAdapter.java:83)
at org.dbunit.dataset.xml.FlatXmlWriter.write(FlatXmlWriter.java:124)
at org.dbunit.dataset.xml.FlatXmlDataSet.write(FlatXmlDataSet.java:378)
at DatabaseExport.main(DatabaseExport.java:23)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'user'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
at org.dbunit.database.AbstractResultSetTable.<init>(AbstractResultSetTable.java:110)
at org.dbunit.database.ForwardOnlyResultSetTable.<init>(ForwardOnlyResultSetTable.java:59)
at org.dbunit.database.CachedResultSetTableFactory.createTable(CachedResultSetTableFactory.java:60)
at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:319)
... 5 more
My guess is that you have a table called
user
in your database. Butuser
is a reserved keyword in MSSQL. DBUnit creates some query like:But this creates the error you are seeing. Instead DBUnit has to create a query like:
I think you could fix this using the Escape pattern configuration setting of DBUnit. I didn't try this myself but something like this should work:
I hope this helps! :)
Thanks for above solution. That is the actual solution.
If you want to export the dataset usnig ant we can do like this
<dbunit driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=config1" userid="user" password="passwd$2004" escapePattern="[?]" datatypeFactory="org.dbunit.ext.mssql.MsSqlDataTypeFactory">
<compare src="C:/export.xml"/> </dbunit>