将数据集导出到 xml 文件,在 DBunit 中出现错误

发布于 2024-12-22 21:05:54 字数 3071 浏览 1 评论 0原文

大家好,我正在使用 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-12-29 21:05:55

感谢以上解决方案。这就是实际的解决方案。

import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.database.DatabaseConfig;
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://localhost:1433;DatabaseName=def_config","user","passwd$2004");
        IDatabaseConnection connection = new DatabaseConnection( jdbcConnection );
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN , "[?]");
        // full database export
        IDataSet fullDataSet = connection.createDataSet();
        FlatXmlDataSet.write(fullDataSet, new FileOutputStream("C:/full.xml"));
    }
}

Thanks for above solution. That is the actual solution.

import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.database.DatabaseConfig;
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://localhost:1433;DatabaseName=def_config","user","passwd$2004");
        IDatabaseConnection connection = new DatabaseConnection( jdbcConnection );
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN , "[?]");
        // full database export
        IDataSet fullDataSet = connection.createDataSet();
        FlatXmlDataSet.write(fullDataSet, new FileOutputStream("C:/full.xml"));
    }
}
邮友 2024-12-29 21:05:55

如果您想使用 ant 导出数据集,我们可以这样做


<比较 src="C:/export.xml"/>

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>

风追烟花雨 2024-12-29 21:05:54

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

SELECT * FROM user;

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

SELECT * from [user];

我认为您可以使用 DBUnit 的 转义模式 配置设置来修复此问题。我自己没有尝试过,但类似的东西应该有效:

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

我希望这有帮助! :)

My guess is that you have a table called user in your database. But user is a reserved keyword in MSSQL. DBUnit creates some query like:

SELECT * FROM user;

But this creates the error you are seeing. Instead DBUnit has to create a query like:

SELECT * from [user];

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:

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

I hope this helps! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文