java String和Clob转换

第一时间想到spring,因为spring对orm工具有封装, 像ibatis,hibernate等,在spring.jar中大概翻了一下包,根据包名和类名发现如下可疑类org.springframework.orm.ibatis.support.ClobStringTypeHandler 根据源码跟踪到了 org.springframework.jdbc.support.lob.OracleLobHandler 这个类才是内有乾坤,有我想要的一切东西,嘿嘿,不好意思了,统统抄来.

在spring包中有个org.springframework.jdbc.support.lob.AbstractLobHandler这里面定义了基本的Clob和Blog处理方法

org.springframework.jdbc.support.lob.DefaultLobHandler是默认的实现,除了Oracle其他数据库使用此实现

org.springframework.jdbc.support.lob.OracleLobHandler是专门用于Oracle的实现,可见Oracle的BT,为了不造成直接包依赖,相关的调用都是利用反射完成的.有兴趣的朋友可以去阅读下这两个类,以便了解Oracle的特殊性.

经过分析,将Clob的创建,以及与String的互转代码放出来供大家使用.当然不可能完全copy了,我做了些简化处理,不过已经测试了,没用问题.更多细节请查阅spring的org.springframework.jdbc.support.lob包源码.

SqlUtil类是我为了方便测试写的,大家靠代码就能够猜出来其内容了,所以就不全贴了,只贴本文的主题部分.



    /**
     *
     * Description:创建Clob或者Blob
     *
     * @param conn数据库连接对象
     * @param lobClassName
     *            oracle.sql.CLOB或者oracle.sql.BLOB
     * @return oracle.sql.CLOB或者oracle.sql.BLOB对象
     * @throws Exception
     * @blog blog.csdn.ne t/sunyujia/
     * @mail [email protected]
     * @since:Oct 1, 2008 6:42:08 PM
     */
    public static Object createOracleLob(Connection conn, String lobClassName)
            throws Exception {
        Class lobClass = conn.getClass().getClassLoader().loadClass(
                lobClassName);
        final Integer DURATION_SESSION = new Integer(lobClass.getField(
                "DURATION_SESSION").getInt(null));
        final Integer MODE_READWRITE = new Integer(lobClass.getField(
                "MODE_READWRITE").getInt(null));
        Method createTemporary = lobClass.getMethod("createTemporary",
                new Class[] { Connection.class, boolean.class, int.class });
        Object lob = createTemporary.invoke(null, new Object[] { conn, false,
                DURATION_SESSION });
        Method open = lobClass.getMethod("open", new Class[] { int.class });
        open.invoke(lob, new Object[] { MODE_READWRITE });
        return lob;
    }

    /**
     *
     * Description:将Clob对象转换为String对象,Blob处理方式与此相同
     *
     * @param clob
     * @return
     * @throws Exception
     * @mail [email protected]
     * @blog blog.csdn.ne t/sunyujia/
     * @since:Oct 1, 2008 7:19:57 PM
     */
    public static String oracleClob2Str(Clob clob) throws Exception {
        return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
    }

    /**
     *
     * Description:将string对象转换为Clob对象,Blob处理方式与此相同
     *
     * @param str
     * @param lob
     * @return
     * @throws Exception
     * @mail [email protected]
     * @blog blog.csdn.ne t/sunyujia/
     * @since:Oct 1, 2008 7:20:31 PM
     */
    public static Clob oracleStr2Clob(String str, Clob lob) throws Exception {
        Method methodToInvoke = lob.getClass().getMethod(
                "getCharacterOutputStream", (Class[]) null);
        Writer writer = (Writer) methodToInvoke.invoke(lob, (Object[]) null);
        writer.write(str);
        writer.close();
       &n-

bsp;return lob;
    }

    /**
     *
     * Description: 全部源码查考自
     * org.springframework.jdbc.support.lob.OracleLobHandler
     *
     * @param args
     * @throws Exception
     * @mail [email protected]
     * @blog blog.csdn.ne t/sunyujia/
     * @since:Oct 1, 2008 7:26:16 PM
     */
    public static void main(String[] args) throws Exception {
        //创建数据源略
        Connection conn = SqlUtil.getConnection();
        Clob clob = (Clob) createOracleLob(conn, "oracle.sql.CLOB");// BLOB的话传oracle.sql.BLOB
        // create table testTb (TheClob Clob);
        PreparedStatement pstmt = conn
                .prepareStatement("insert into testTb (TheClob) values (?)");
        pstmt.setClob(1, oracleStr2Clob("test", clob));
        pstmt.execute();
        SqlUtil.closeStmt(pstmt);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from testTb");
        while (rs.next()) {
            String str = oracleClob2Str(rs.getClob(1));
            System.out.println(str);
        }
        SqlUtil.closeRs(rs);
        SqlUtil.closeStmt(stmt);
        SqlUtil.closeConn(conn);
    }

你可能感兴趣的:(MyEclipse,Oracle,JAVA技术整理)