2017-05-03 66 views
0

我如何通過XML文檔從Java到CallableStatement傳遞XML到Oracle程序的Java(ojdbc)

代碼,我準備XML文檔:

DocumentBuilderFactory dbFactory = 
DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = 
dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.newDocument(); 

// root element 
org.w3c.dom.Element rootElement = doc.createElement("data"); 
doc.appendChild(rootElement); 

org.w3c.dom.Element object_id = doc.createElement("object_id"); 
rootElement.appendChild(object_id); 
object_id.appendChild(createElement(doc, "attribuName1", "value1")); 
object_id.appendChild(createElement(doc, "attribuName2", "value2")); 

XML結構:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<data> 
    <object_id> 
     <field name="name2" value="2"/> 
     <field name="name1" value="1"/> 
    </object_id> 
</data> 

現在我創建CallableStatement來調用過程並傳遞一些值以及我想在這裏傳遞XML文檔。

CallableStatement cstmt = conn.prepareCall("{? = CALL SCHEMA.PROC_DATA_XML(?,?,?)}"); 
cstmt.setString(1, "StringParam1"); 
cstmt.setInt(2, 9999); 
//!this is where i want to pass that XML document. So how should i do this? I tried this one: 
cstmt.setObject(3, doc); 

cstmt.registerOutParameter(1, Types.INTEGER); 
cstmt.executeUpdate(); 
float result = cstmt.getInt(1); 

程序代碼:

CREATE OR REPLACE PROCEDURE PROC_DATA_XML(
    ID IN SCHEMA.XML.ID%TYPE ,--varchar(4) 
    ID2 IN SCHEMA.XML.ID2%TYPE ,--number(4) 
    RESULT OUT SCHEMA.XML.RESULT%TYPE, -- 0 – OK 1 – NOT OK 
    XML_DATA IN SCHEMA.XML.DATA_CONTENT%TYPE --XML File 
    ) IS 
BEGIN 

也許有人可以給我提供的是如何做到這一點在Java另一種方式?

+0

請在問題'SCHEMA.XML'結構中顯示。 – Seyran

+0

@Seyran我加了 – arccuks

+0

如果我在你那裏,我會通過最後一個參數作爲CLOB。 – Seyran

回答