2011-04-05 58 views

回答

10

擁有約100行的代碼;-) Here is an example

要點:與其他JDBC驅動程序不同,Oracle提供的不支持使用ReaderInputStream作爲INSERT的參數。相反,你必須SELECTCLOBFOR UPDATE然後寫入到ResultSet

我建議你提出這個代碼到一個輔助方法/類。否則,它會污染你的其他代碼。

+0

+1,提示幫助者類。 – asgs 2011-04-05 09:02:30

20

最簡單的方法是簡單地使用

stmt.setString(position, xml); 

方法(「小」的字符串,其可以很容易地保持在Java內存),或

try { 
    java.sql.Clob clob = 
    oracle.sql.CLOB.createTemporary(
     connection, false, oracle.sql.CLOB.DURATION_SESSION); 

    clob.setString(1, xml); 
    stmt.setClob(position, clob); 
    stmt.execute(); 
} 

// Important! 
finally { 
    clob.free(); 
} 
+0

嗯,你說得對。我最近沒有嘗試過。我會用另一個 – 2011-04-05 09:03:50

+0

來更新我的迴應,這裏有什麼用的oracle.sql.CLOB.DURATION_SESSION? – 2013-11-14 13:14:47

+0

@ V-spring:Oracle「CLOB」是一個「連接」對象,其生命週期獨立於對其進行實際引用的查詢。據我所知,即使執行了'INSERT' /'UPDATE',你也可以讀/寫實際的LOB。我不得不尋找細節,以確保這一點,雖然... – 2013-11-14 13:31:50

2

爲此,你需要建立連接的結果集

ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE

Connection con=null; 
//initialize connection variable to connect to your database... 
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
String query="Select MYCLOB from TABLE_NAME for update"; 
con.setAutoCommit(false); 
ResultSet resultset=stmt.executeQuery(query); 


if(resultset.next()){ 
oracle.sql.CLOB clobnew = ((OracleResultSet) rss).getCLOB("MYCLOB"); 
PrintWriter pw = new PrintWriter(clobnew.getCharacterOutputStream()); 
BufferedReader br = new BufferedReader(new FileReader(new File("filename.xml"))); 
String lineIn = null; 
while((lineIn = br.readLine()) != null) 
     pw.println(lineIn); 
     pw.close(); 
     br.close(); 
} 

con.setAutoCommit(true); 
con.commit(); 
} 

注:它,你加上去的重要更新在寫入的查詢結束處選擇行...

跟隨t他上面的代碼中插入XML文件

2

轉換CLOB到字符串:

Clob clob=rs.getClob(2);  
String str=(String)clob.getSubString(1,(int)clob.length());  
System.out.println("Clob Data is : "+str); 
2

可以很好地與下面的代碼做,我給你剛纔的代碼插入XML希望你與其他做的其他東西..

import oracle.xdb.XMLType; 

//now inside the class...... 
// this will be to convert xml into string 

File file = new File(your file path); 
FileReader fileR = new FileReader(file); 
fileR.read(data); 
String str = new String(data); 

// now to enter it into db 

conn = DriverManager.getConnection(serverName, userId, password); 

XMLType objXml = XMLType.createXML(conn, str); 

// inside the query statement put this code 

objPreparedstatmnt.setObject(your value index, objXml); 

我已經這樣做,它工作正常。

4

將xml內容作爲字符串傳遞。

table1 

ID int 
XML CLOB 



import oracle.jdbc.OraclePreparedStatement; 
/* 
Your Code 
*/ 
void insert(int id, String xml){ 
    try { 
     String sql = "INSERT INTO table1(ID,XML) VALUES (" 
       + id 
       + "', ?)"; 
     PreparedStatement ps = conn.prepareStatement(sql); 
     ((OraclePreparedStatement) ps).setStringForClob(1, xml); 
     ps.execute(); 
     result = true; 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
+1

[setStringForClob] +1(http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OraclePreparedStatement.html#setStringForClob_int__java_lang_String_) – ceving 2013-06-04 11:37:32

4

此代碼適用於我。我使用ojdbc6-11.2.0.2.jar。

java.sql.Connection con; 
javax.xml.bind.Marshaller marshaller; 

Clob xmlClob = con.createClob(); 
try { 
    try (Writer xmlClobWriter = xmlClob.setCharacterStream(1)) { 
    m.marshal(jaxbObject, xmlClobWriter); 
    } // xmlClobWriter.close(); 
    try (PreparedStatement stmt = con.prepareStatement("INSERT INTO table (xml) values(?)")) { 
    stmt.setClob(1, xmlClob); 
    stmt.executeUpdate(); 
    } 
} finally { 
    xmlClob.free(); 
} 
相關問題