Popular Posts
DNS SERVER LIST Google 8.8.8.8 8.8.4.4 TWNIC 192.83.166.11 211.72.210.250 HiNet 168.95.1.1 168.95.192.1 Seednet 北區 DNS (台北, 桃園, 新竹, 宜蘭, 花蓮, 苗栗) 139.... write 語法: write at [/][<pos>][(<len>)] output 用法: write 'Bruce'. " 輸出常數 Bruce data CNAME(10) value... Regex (Regular Expression) tool Eclipse Regular Expression Tester Java正規表示式工具-Eclipse GUI介面 [Visual Studio 2010] 好用的 Regex 輔助工具
Blog Archive
Stats
LOB column read/write in Oracle
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.CLOB;

public class OracleLobType {

    public static void insertLOB() throws ClassNotFoundException, SQLException, IOException {

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("ConnectionString");

        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "";

        conn.setAutoCommit(false);
        sql = "INSERT INTO Table(id, clobColumn) VALUES(?, empty_clob())";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, 1);
        pstmt.executeUpdate();
        pstmt.close();

        pstmt = null;
        sql = "SELECT clobColumn FROM Table WHERE id = 1 FOR UPDATE";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();

        CLOB clobValue = null;
        while (rs.next()) {
            clobValue = (CLOB) rs.getClob("clobColumn");
        }
        Writer wr = clobValue.getCharacterOutputStream();
        wr.write("clob data");
        wr.flush();
        wr.close();

        rs.close();
        pstmt.close();
        conn.commit();
        conn.close();

    }

    public static void getLOG() throws ClassNotFoundException, SQLException {

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("ConnectionString");

        String sql = "";
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        sql = "SELECT clobColumn FROM Table WHERE id = 1";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();

        CLOB clobValue = null;
        while (rs.next()) {
            OracleResultSet ors = (OracleResultSet) rs;
            clobValue = (CLOB) ors.getCLOB("clobColumn");
        }

        String value = "";
        if (clobValue != null && clobValue.length() > 0)
            // clobValue.stringValue();
            value = clobValue.getSubString(1, (int) clobValue.length());

        rs.close();
        pstmt.close();
        conn.close();

    }
}