通过java中的存储过程提取XML自动数据(MS SQL)

时间:2015-07-02 11:03:19

标签: java sql-server stored-procedures

我使用以下查询通过stored-procedure返回

SELECT * FROM test FOR XML AUTO

当我运行存储过程

时,此inturn给出了以下结果

服务器中的OUTPUT:

<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
.........

当我尝试通过java中的preparecall执行该过程,并且我尝试返回结果集时,这应该返回一个xml字符串,但我得到如下所示的分割,

Record1:
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data
Record 2: 1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test colu
.............. and so on.

以下是我的代码,

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("{call test_proc()}");
ResultSet rs = statement.getResultSet();

while(rs.next()){
        c++;
        System.out.println("Record "+c+":"+rs.getString(1));
}
System.out.println("Count : "+c);

这是返回计数3而不是1,但是当我将其作为匿名过程运行时,它在sql server中运行正常。

我也尝试了getSQLXML(int column)方法,但没有用。

有人可以请你帮忙吗?

再添一点:我尝试了简单的查询执行(通过java代码) - SELECT * FROM test FOR XML AUTO并尝试获取结果集,但它仍提供相同的结果。 :(

1 个答案:

答案 0 :(得分:1)

添加ELEMENTS以及列都在同一个表中。

SELECT * FROM test FOR XML AUTO ,ELEMENTS