如何使用spring jdbcTemplate从storedProcedudure接收XML参数?

时间:2014-10-31 11:30:03

标签: java sql-server spring jdbctemplate

我在我的spring应用程序中使用MS-SQL服务器。 我有一个商店程序,我想从我的应用程序执行。

CREATE PROCEDURE dbo.spCheckPerson (
    @inXML XML,
    @outXML XML OUTPUT,
    @inDebug BIT = 0 ) 
AS
    --do sth

SET @outXML = '<person><name>Piotr</name></person>'

RETURN 0 

我尝试通过这个DAO执行它:

import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Types;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.support.xml.Jdbc4SqlXmlHandler;

public class PersonDAOImpl implements PersonDAO {

    private JdbcTemplate jdbcTemplate;
    private SimpleJdbcCall spCheckPerson;

    public PersonDAOImpl(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public String checkPersonData(String personData) {
        Jdbc4SqlXmlHandler sqlXmlHandler = new Jdbc4SqlXmlHandler();
        spCheckPerson = new SimpleJdbcCall(jdbcTemplate)
                .withoutProcedureColumnMetaDataAccess()
                .withProcedureName("spCheckPerson")
                .declareParameters(new SqlParameter("inXML", Types.SQLXML))
                .declareParameters(new SqlOutParameter("outXML", Types.SQLXML));
        Map<String, Object> result = spCheckPerson.execute(sqlXmlHandler.newSqlXmlValue("<test>Test</test>"));
        return ((SQLXML) result.get("outXML")).getString();
    }

}

,但我收到错误:

SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path 

[/TestSpringProject] threw exception [Request processing failed; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The TDS protocol stream is not valid.] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: The TDS protocol stream is not valid.
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.throwInvalidTDS(SQLServerConnection.java:1635)
    at com.microsoft.sqlserver.jdbc.TDSReader.throwInvalidTDS(IOBuffer.java:4747)
    at com.microsoft.sqlserver.jdbc.TDSReader.readBytes(IOBuffer.java:5061)
    at com.microsoft.sqlserver.jdbc.TDSReader.readWrappedBytes(IOBuffer.java:5082)
    at com.microsoft.sqlserver.jdbc.TDSReader.readInt(IOBuffer.java:5023)
    at com.microsoft.sqlserver.jdbc.TDSReader.readUnsignedInt(IOBuffer.java:5040)
    at com.microsoft.sqlserver.jdbc.PLPInputStream.readBytesInternal(PLPInputStream.java:313)
    at com.microsoft.sqlserver.jdbc.PLPInputStream.getBytes(PLPInputStream.java:104)
    at com.microsoft.sqlserver.jdbc.PLPXMLInputStream.getBytes(PLPInputStream.java:509)
    at com.microsoft.sqlserver.jdbc.SQLServerSQLXML.getString(SQLServerSQLXML.java:290)
    at pl.test.dao.PersonDAOImpl.checkPersonData(PersonDAOImpl.java:34)

我该如何正确接收这个物体?我应该使用哪个类来施展价值?什么是TDS协议?我尝试使用SQLXML中的方法:getCharacterStream(),但它给出了相同的异常。

0 个答案:

没有答案