Oracle认证:Clob与String类型转换& \ E5 m% d8 q% S
在论坛上看到的一个问题,其实我从来没有用过Clob,因为确实没这个需求,但是为了抢分,如何最快的找到解决方案呢,第一时间想到 spring,因为spring对orm工具有封装, 像ibatis,hibernate等,在spring.jar中大概翻了一下包,根据包名和类名发现如下可疑类 org.springframework.orm.ibatis.support.ClobStringTypeHandler 根据源码跟踪到了 org.springframework.jdbc.support.lob.OracleLobHandler 这个类才是内有乾坤,有我想要的一切东西,嘿嘿,不好意思了,统统抄来.
8 M6 e0 h4 p9 x 在spring包中有个org.springframework.jdbc.support.lob.AbstractLobHandler这里面定义了基本的Clob和Blog处理方法
( F. Y& }9 m2 b1 @ org.springframework.jdbc.support.lob.DefaultLobHandler是默认的实现,除了Oracle其他数据库使用此实现 b$ G; j1 t6 a, N& _ A
org.springframework.jdbc.support.lob.OracleLobHandler是专门用于Oracle的实现,可见Oracle的BT,为了不造成直接包依赖,相关的调用都是利用反射完成的.有兴趣的朋友可以去阅读下这两个类,以便了解Oracle的特殊性.8 N8 i6 l; V T8 T1 B
经过分析,将Clob的创建,以及与String的互转代码放出来供大家使用.当然不可能完全copy了,我做了些简化处理,不过已经测试了,没用问题.更多细节请查阅spring的org.springframework.jdbc.support.lob包源码.. n5 H; R* s/ ^' z7 ]
来源:(http://blog.sina.com.cn/s/blog_5109612b0100aq6l.html) - Oracle中的Clob与String类型转换_镭射头_新浪博客' u0 ]# o0 ^2 n+ X1 @' V
SqlUtil类是我为了方便测试写的,大家靠代码就能够猜出来其内容了,所以就不全贴了,只贴本文的主题部分.
# W/ L6 g1 F. g4 s2 C C public static Object createOracleLob(Connection conn, String lobClassName)
3 F* [. w' w9 ~3 G throws Exception {
; V7 ~. C7 }( n# R- x- X) H Class lobClass = conn.getClass().getClassLoader().loadClass(
4 R2 m7 o5 M$ I% f2 O% g1 C* { lobClassName);5 f8 g6 G0 t$ p! L9 X
final Integer DURATION_SESSION = new Integer(lobClass.getField(+ h2 u/ {8 O$ a0 {
"DURATION_SESSION").getInt(null));! A2 L5 ~' W4 p0 F5 _5 [
final Integer MODE_READWRITE = new Integer(lobClass.getField(
; Q$ n% P& l4 O) h& c "MODE_READWRITE").getInt(null));
1 H x y6 d& k: q" | Method createTemporary = lobClass.getMethod("createTemporary",
, t1 r; w1 x9 K q6 u: v: s' U1 O new Class[] { Connection.class, boolean.class, int.class });
1 A+ m Q9 O$ T/ J9 l Object lob = createTemporary.invoke(null, new Object[] { conn, false,
4 l: w9 V7 F& V2 t4 y DURATION_SESSION });
- o0 \. W. T# v/ f" f Method open = lobClass.getMethod("open", new Class[] { int.class });
" F& q3 e2 F& J7 o0 ^3 y7 S open.invoke(lob, new Object[] { MODE_READWRITE });, L, g- A& S1 K7 Q# o! O
return lob;
7 @, R& E' v# l6 D/ V/ h } |