国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > Oracle > 正文

展示JDBC存取ORACLE大型數(shù)據(jù)對(duì)象LOB幾種情況的示范類

2024-08-29 13:49:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
    展示JDBC存取Oracle大型數(shù)據(jù)對(duì)象LOB幾種情況的示范類import java.io.*;
import java.util.*;
import java.sql.*;public class LobPRos
{    /**
     * ORACLE驅(qū)動(dòng)程序
     */
    private static final 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";    /**
     * 數(shù)據(jù)庫(kù)連接
     */
    private static Connection conn = null;    /**
     * SQL語(yǔ)句對(duì)象
     */
    private static Statement stmt = null;    /**
     * @roseuid 3EDA089E02BC
     */
    public LobPros()
    {    }    /**
     * 往數(shù)據(jù)庫(kù)中插入一個(gè)新的CLOB對(duì)象
     *
     * @param infile - 數(shù)據(jù)文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04A902BC
     */
    public static void clobInsert(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 插入一個(gè)空的CLOB對(duì)象 */
            stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
            /* 查詢此CLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
            while (rs.next()) {

                /* 取出此CLOB對(duì)象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 向CLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
                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) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * 修改CLOB對(duì)象(是在原CLOB對(duì)象基礎(chǔ)上進(jìn)行覆蓋式的修改)
     *
     * @param infile - 數(shù)據(jù)文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04B60367
     */
    public static void clobModify(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 查詢CLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");

            while (rs.next()) {
                /* 獲取此CLOB對(duì)象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 進(jìn)行覆蓋式修改 */
                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) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * 替換CLOB對(duì)象(將原CLOB對(duì)象清除,換成一個(gè)全新的CLOB對(duì)象)
     *
     * @param infile - 數(shù)據(jù)文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04BF01E1
     */
    public static void clobReplace(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 清空原CLOB對(duì)象 */

            stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
            /* 查詢CLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
            while (rs.next()) {
                /* 獲取此CLOB對(duì)象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 更新數(shù)據(jù) */
                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) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * CLOB對(duì)象讀取
     *
     * @param outfile - 輸出文件名
     * @throws java.lang.Exception
     * @roseuid 3EDA04D80116
     */
    public static void clobRead(String outfile) throws Exception

    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 查詢CLOB對(duì)象 */
            ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
            while (rs.next()) {
                /* 獲取CLOB對(duì)象 */
                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
                /* 以字符形式輸出 */
                BufferedReader in = new BufferedReader(clob.getCharacterStream());
                BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                out.close();
                in.close();
            }
        } catch (Exception ex) {
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * 向數(shù)據(jù)庫(kù)中插入一個(gè)新的BLOB對(duì)象
     *
     * @param infile - 數(shù)據(jù)文件
     * @throws java.lang.Exception
     * @roseuid 3EDA04E300F6
     */
    public static void blobInsert(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();

        conn.setAutoCommit(false);        try {
            /* 插入一個(gè)空的BLOB對(duì)象 */
            stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
            /* 查詢此BLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
            while (rs.next()) {
                /* 取出此BLOB對(duì)象 */
                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
                /* 向BLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
                BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */
            conn.commit();
        } catch (Exception ex) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * 修改BLOB對(duì)象(是在原BLOB對(duì)象基礎(chǔ)上進(jìn)行覆蓋式的修改)
     *
     * @param infile - 數(shù)據(jù)文件

     * @throws java.lang.Exception
     * @roseuid 3EDA04E90106
     */
    public static void blobModify(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 查詢BLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
            while (rs.next()) {
                /* 取出此BLOB對(duì)象 */
                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
                /* 向BLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
                BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */
            conn.commit();
        } catch (Exception ex) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);

    }    /**
     * 替換BLOB對(duì)象(將原BLOB對(duì)象清除,換成一個(gè)全新的BLOB對(duì)象)
     *
     * @param infile - 數(shù)據(jù)文件
     * @throws java.lang.Exception
     * @roseuid 3EDA0505000C
     */
    public static void blobReplace(String infile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 清空原BLOB對(duì)象 */
            stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
            /* 查詢此BLOB對(duì)象并鎖定 */
            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
            while (rs.next()) {
                /* 取出此BLOB對(duì)象 */
                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
                /* 向BLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
                BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */
            conn.commit();
        } catch (Exception ex) {

            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * BLOB對(duì)象讀取
     *
     * @param outfile - 輸出文件名
     * @throws java.lang.Exception
     * @roseuid 3EDA050B003B
     */
    public static void blobRead(String outfile) throws Exception
    {
        /* 設(shè)定不自動(dòng)提交 */
        boolean defaultCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);        try {
            /* 查詢BLOB對(duì)象 */
            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
            while (rs.next()) {
                /* 取出此BLOB對(duì)象 */
                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
                /* 以二進(jìn)制形式輸出 */
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
                BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
                int c;
                while ((c=in.read())!=-1) {
                    out.write(c);
                }
                in.close();
                out.close();
            }
            /* 正式提交 */

            conn.commit();
        } catch (Exception ex) {
            /* 出錯(cuò)回滾 */
            conn.rollback();
            throw ex;
        }        /* 恢復(fù)原提交狀態(tài) */
        conn.setAutoCommit(defaultCommit);
    }    /**
     * 建立測(cè)試用表格
     * @throws Exception
     */
    public static void createTables() throws Exception {
        try {
            stmt.executeUpdate("CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)");
            stmt.executeUpdate("CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)");
        } catch (Exception ex) {        }
    }    /**
     * @param args - 命令行參數(shù)
     * @throws java.lang.Exception
     * @roseuid 3EDA052002AC
     */
    public static void main(String[] args) throws Exception
    {
        /* 裝載驅(qū)動(dòng),建立數(shù)據(jù)庫(kù)連接 */
        Class.forName(DRIVER);
        conn = DriverManager.getConnection(URL,USER,PASSWORD);
        stmt = conn.createStatement();        /* 建立測(cè)試表格 */
        createTables();        /* CLOB對(duì)象插入測(cè)試 */
        clobInsert("c:/clobInsert.txt");
        clobRead("c:/clobInsert.out");        /* CLOB對(duì)象修改測(cè)試 */
        clobModify("c:/clobModify.txt");
        clobRead("c:/clobModify.out");        /* CLOB對(duì)象替換測(cè)試 */
        clobReplace("c:/clobReplace.txt");
        clobRead("c:/clobReplace.out");        /* BLOB對(duì)象插入測(cè)試 */
        blobInsert("c:/blobInsert.doc");
        blobRead("c:/blobInsert.out");        /* BLOB對(duì)象修改測(cè)試 */

        blobModify("c:/blobModify.doc");
        blobRead("c:/blobModify.out");        /* BLOB對(duì)象替換測(cè)試 */
        blobReplace("c:/blobReplace.doc");
        blobRead("c:/bolbReplace.out");        /* 關(guān)閉資源退出 */
        conn.close();
        System.exit(0);
    }
}


上一篇:優(yōu)化Oracle .Net配置

下一篇:oracle中如何刪除重復(fù)數(shù)據(jù)

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 炎陵县| 略阳县| 简阳市| 平利县| 九寨沟县| 苏尼特右旗| 简阳市| 故城县| 盱眙县| 玉溪市| 昆山市| 舞钢市| 镇康县| 宁南县| 桑植县| 杨浦区| 韶关市| 华坪县| 商丘市| 资溪县| 慈溪市| 洛隆县| 永康市| 通山县| 花垣县| 桦南县| 忻城县| 那坡县| 锡林郭勒盟| 叶城县| 龙口市| 白银市| 巨野县| 桂平市| 西藏| 赤城县| 寿阳县| 定结县| 四平市| 富源县| 舒兰市|