大家好,我正在使用dbunit。我正在尝试将db的数据集导出到xml文件中。

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"));

    }
}


但是它给出了这样的错误:

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

最佳答案

我的猜测是您的数据库中有一个名为user的表。但是user是MSSQL中的保留关键字。 DBUnit创建一些查询,例如:

SELECT * FROM user;


但这会产生您所看到的错误。相反,DBUnit必须创建类似以下的查询:

SELECT * from [user];


我认为您可以使用DBUnit的Escape pattern配置设置解决此问题。我自己没有尝试过,但是类似的东西应该可以工作:

connection.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN , "[?]");


我希望这有帮助! :)

关于java - 将数据集导出到xml文件中,DBunit中出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8633372/

10-09 03:34