Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Hierarchical Query Start with connect by prior 階層式查詢用法 SELECT s.role_id, s.role_name, s.role_base_on, b.role_name role_base_on_name FROM m_usr_role s... Tired of Hibernate? Try JDBI in your code JDBI Quick sample ICategoryDAO.java : create a data access interface (implement is not required) package com.prhythm.erotic.task.data....
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();

    }
}