Java 儲存和讀取 oracle CLOB 類型欄位的實用方法

來源:互聯網
上載者:User

標籤:

 package oracle.clob; import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.Reader; 
import java.io.StringReader; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import oracle.jdbc.driver.OracleDriver; 
import oracle.sql.CLOB; 

public class ClobTest { 
    String url = "jdbc:oracle:thin:@192.168.2.157:1521:orcl"; 
    String user = "xj"; 
    String pwd = "xj"; 
    String text = "這是要插入到CLOB裡面的資料"; 

    
    private void clobImport() throws ClassNotFoundException, SQLException { 
        // TODO Auto-generated method stub 
        DriverManager.registerDriver(new OracleDriver()); 
        Connection conn = DriverManager.getConnection(url, user, pwd);// 得到連線物件 
        String sql = "insert into clob_test(id,str) values (‘1‘,?)";// 要執行的SQL語句 

        PreparedStatement stmt = conn.prepareStatement(sql);// 載入SQL語句 
        // PreparedStatement支援SQL帶有問號?,可以動態替換?的內容。 
        Reader clobReader = new StringReader(text); // 將 text轉成流形式 
        stmt.setCharacterStream(1, clobReader, text.length());// 替換sql語句中的? 
        int num = stmt.executeUpdate();// 執行SQL 
        if (num > 0) { 
            System.out.println("ok"); 
        } else { 
            System.out.println("NO"); 
        } 
        stmt.close(); 
        conn.close(); 
    } 

    private void clobExport() throws ClassNotFoundException, SQLException, 
            IOException { 
        // TODO Auto-generated method stub 
        CLOB clob = null; 
        String sql = "select * from clob_test where id=1"; 
        DriverManager.registerDriver(new OracleDriver()); 
        Connection conn = DriverManager.getConnection(url, user, pwd);// 得到連線物件 
        PreparedStatement stmt = conn.prepareStatement(sql); 
        ResultSet rs = stmt.executeQuery(); 
        String id = ""; 
        String content = ""; 
        if (rs.next()) { 
            id = rs.getString("id");// 獲得ID 
            clob = (oracle.sql.CLOB) rs.getClob("str"); // 獲得CLOB欄位str 
            // 注釋: 用 rs.getString("str")無法得到 資料 ,返回的 是 NULL; 
            content = ClobToString(clob); 
        } 
        stmt.close(); 
        conn.close(); 
        // 輸出結果 
        System.out.println(id); 
        System.out.println(content); 
    } 

    // 將字CLOB轉成STRING類型 
    public String ClobToString(CLOB clob) throws SQLException, IOException { 

        String reString = ""; 
        Reader is = clob.getCharacterStream();// 得到流 
        BufferedReader br = new BufferedReader(is); 
        String s = br.readLine(); 
        StringBuffer sb = new StringBuffer(); 
        while (s != null) {// 執行迴圈將字串全部取出付值給StringBuffer由StringBuffer轉成STRING 
            sb.append(s); 
            s = br.readLine(); 
        } 
        reString = sb.toString(); 
        return reString; 
    } 

    
    // TODO Auto-generated method stub 
    public static void main(String[] args) throws IOException, 
            ClassNotFoundException, SQLException { 
        // TODO Auto-generated method stub 
        ClobTest clobtest = new ClobTest(); 
        // read file 
        FileReader _frd = new FileReader(new File("D:\\DOS.txt")); 
        BufferedReader _brd = new BufferedReader(_frd); 
        String _rs = _brd.readLine(); 
        StringBuffer _input = new StringBuffer(); 
        while (_rs != null) { 
            _input.append(_rs); 
            _rs = _brd.readLine(); 
        } 
        // System.out.println(_input.toString()); 
        // 輸入測試 
        clobtest.text = _input.toString(); 
        clobtest.clobImport(); 
        // 輸出測試 
        // clobtest.clobExport(); 
    } 

}

Java 儲存和讀取 oracle CLOB 類型欄位的實用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.