SQLXML不能用于java

时间:2015-03-24 09:25:34

标签: java sql-server xml jsp sqlxml

环境详情如下: -

  

Appserver是: - apache tomcat 8 jre 1.8(最新于2015年3月1日)   数据库是MS-SQL Server 2008

以下是代码段: -

<%@ page import="java.util.*,java.io.*,org.xml.sax.* ,javax.xml.*, org.w3c.dom.*, javax.sql.*, javax.xml.transform.*, javax.xml.transform.sax.* "%>

<% 
java.sql.Connection con;
java.sql.Statement s;
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;
con=null;s=null;pst=null;rs=null;
//String url= "jdbc:sqlserver://dB_IP/dBName"; 
String url= "jdbc:sqlserver://IPADDRESS;databaseName=dBName;integratedSecurity=false;user=userid;password=password;";
//String url= "jdbc:jtds:sqlserver://dB_IP/dBName";
String id= "user_id";
String pass = "password";
try{
//Class.forName("net.sourceforge.jtds.jdbc.Driver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//con = java.sql.DriverManager.getConnection(url, id, pass);
con = java.sql.DriverManager.getConnection(url);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = null,root1=null,root2=null,content_type="text/xml";
if(request.getParameter("sql")!=null){sql=request.getParameter("sql");}else{sql="select * from test_table for xml raw, xmldata";}
if(request.getParameter("root")!=null){root1=request.getParameter("root");root2="</"+root1+">";root1="<"+root1+">";}else{root1="<parse>";root2="</parse>";}
if(request.getParameter("contenttype")!=null){content_type=request.getParameter("contenttype");}else{content_type="text/xml";}
try{
    out.println( "inside try" );
    s = con.createStatement();
    //SQLXML xmlVal = con.createSQLXML();
    String query = "select top 100 * from table for xml raw, xmldata";
    rs = s.executeQuery(query);
        while (rs.next()) {
            out.println( "<br/>inside while " + query);
            java.sql.SQLXML sqlxml = rs.getSQLXML(1);
            //InputStream binaryStream = sqlxml.getBinaryStream();
/*
            java.sql.SQLXML rssFeedXML = rs.getSQLXML(1);
            javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(rssFeedXML.getBinaryStream());
            //System.out.println("RSS identifier: " + rssName);
            //out.println(JDBCTutorialUtilities.convertDocumentToString(doc));
            out.println( (doc) );
*/
/*
//http://docs.oracle.com/javase/6/docs/api/java/sql/SQLXML.html

            Transformer identity = TransformerFactory.newInstance().newTransformer();
            Source source = sqlxml.getSource(null);
            Result result = new StreamResult(System.out);
            identity.transform(source, result);
*/
        }
        out.flush();out = pageContext.pushBody();
        out.println( "Hi " );
}
catch(Exception e){e.printStackTrace();out.println( "<br/>Caught " + e);}
finally{
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
}

%>

发生编译错误: -

  

抓到com.microsoft.sqlserver.jdbc.SQLServerException:转换   从ntext到SQLXML不受支持。

“和要求是”: - 从select语句返回一个xml文档。 xml应用程序是必需的。

如果有任何建议而非此方法,您可以告诉我。

1 个答案:

答案 0 :(得分:0)

我今天遇到了同样的问题。看起来元数据没有使用“FOR XML”指令正确填充。

为了解决这个问题,我首先使用变量并将xml保存到变量中,如:

DECLARE @tmpXML XML 
SET @tmpXML = (
SELECT * FROM (SELECT 1 as one) T FOR XML AUTO, ELEMENTS)
SELECT @tmpXML as [I can even have a name]

然后可以使用JDBC中的getSQLXML方法获取XML。