日期:2014-05-16  浏览次数:20376 次

JDBC 插入、修改 Oracle Lob字段
String DRIVER = "oracle.jdbc.driver.OracleDriver"; 
    /**
     * ORACLE连接用URL
     */
    private static final String URL = "jdbc:oracle:thin:@test2000:1521:orac";

    /**
     * 用户名
     */
    private static final String USER = "user";

    /**
     * 密码
     */
    private static final String PASSWORD = "pswd";

    /**
     * 数据库连接
     */
    private static Connection conn = null;

    /**
     * SQL语句对象
     */
    private static Statement stmt = null;

    /**
     * @roseuid 3EDA089E02BC
     */
    public LobPros()
    {

    }

    /**
     * 往数据库中插入一个新的CLOB对象
     *
     * @param infile - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     */
    public static void clobInsert(String infile) throws Exception
    {
        /* 设定不自动提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);

        try {
            /* 插入一个空的CLOB对象 */
            stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
            /* 查询此CLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
            while (rs.next()) {
                /* 取出此CLOB对象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 向CLOB对象中写入数据 */
                BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
                BufferedReader in = new BufferedReader(new FileReader(infile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */
            conn.commit();
        } catch (Exception ex) {
            /* 出错回滚 */
            conn.rollback();
            throw ex;
        }

        /* 恢复原提交状态 */
        conn.setAutoCommit(defaultCommit);
    }

    /**
     * 修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)
     *
     * @param infile - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04B60367
     */
    public static void clobModify(String infile) throws Exception
    {
        /* 设定不自动提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);

        try {
            /* 查询CLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
            while (rs.next()) {
                /* 获取此CLOB对象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 进行覆盖式修改 */
                BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
                BufferedReader in = new BufferedReader(new FileReader(infile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */
            conn.commit();
        } catch (Exception ex) {
            /* 出错回滚 */
            conn.rollback();
            throw ex;
        }

        /* 恢复原提交状态 */
        conn.setAutoCommit(defaultCommit);
    }

    /**
     * 替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象)
     *
     * @param infile - 数据文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04BF01E1
     */
    public static void clobReplace(String infile) throws Exception
    {
        /* 设定不自动提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);

        try {
            /* 清空原CLOB对象 */
            stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
            /* 查询CLOB对象并锁定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
            while (rs.next()) {
                /* 获取此CLOB对象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");