2011-04-05 59 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(); 
} 
相关问题