2014-11-21 71 views
2

我想從數據類型爲clob的表中選擇數據。 所以resut就像[email protected] 我想顯示數據爲字符串,因爲它們出現在表中。如何使用Groovy將clob轉換爲字符串?

在常規

我想這:

rowTest = sql.firstRow("select name from table where id=10") 
clobTest = (oracle.sql.CLOB)rowTest[0] 

byte_stream_test = clobTest.getBinaryStream() 
if(byte_stream_test == null) { println "Test: Received null stream!" } 

byte[] byte_array_test = new byte[10] 
int bytes_read_test = byte_stream_test.read(byte_array_test) 

print "Read $bytes_read_test bytes from the clob!" 

sql.connection.close() 

我有以下錯誤:

---- A working test of writing and then reading a blob into an Oracle DB --- 
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: oracle.sql.CLOB.getBinaryStream() is applicable for argument types:() values: [] 
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:56) 
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112) 
    at Test2.run(Test2.groovy:10) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) 
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207) 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016) 
    at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:901) 
    at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:884) 
    at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:406) 
    at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source) 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) 
    at Test2.main(Test2.groovy) 

你能幫我解決這個問題,謝謝

+0

你試過看JavaDaoc爲CLOB http://download.oracle.com/otn_hosted_doc/jdeveloper/905/jdbc-javadoc/oracle/sql/ CLOB.html? – 2014-11-21 23:15:40

回答

7

試試下面的代碼:

rowTest = sql.firstRow("select name from table where id=10") 
    clobTest = (oracle.sql.CLOB)rowTest[0] 
    bodyText = clobTest?.asciiStream.text 
    println bodyText 
+0

如果這樣的話,就沒有必要鑄 – 2014-11-22 00:29:29

+0

我試過上面的代碼,我看不到輸出,現在沒有錯誤信息,日誌輸出內容在哪裏。 – 2014-11-24 14:03:35

+0

clobTest?意味着如果clobTest不爲null,.asciiStream.text將被執行。由於你沒有得到任何輸出,我猜這個clobTest實際上是null。去掉 ?並測試它。 – user987339 2014-11-26 10:59:15

0

使用characterStream而不是asciiStream支持Unicode字符:

String unicode = "äö" 
Clob clob = new ClobImpl(unicode) 
String strClob = clob?.characterStream?.text 
assertEquals(strClob, unicode)