java jdbc存取oracle clob型別

發表於2014-07-09
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
     <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>

相關文章