展示JDBC存取ORACLE大型数据对象LOB几种情况的示范类

  1. package test;   
  2.    
  3. import  java.io.*;   
  4. import  java.util.*;   
  5. import  java.sql.*;   
  6.    
  7. /**   
  8.  * 展示JDBC存取ORACLE大型数据对象LOB几种情况的示范类   
  9.  *   
  10.  * 
  11.  * @version 1.0   
  12.  * 
  13.  */    
  14. public   class  LobPros   
  15. {   
  16.    
  17.    /**   
  18.     * ORACLE驱动程序   
  19.     */    
  20.    private   static   final  String DRIVER =  "oracle.jdbc.driver.OracleDriver" ;   
  21.    
  22.    /**   
  23.     * ORACLE连接用URL   
  24.     */    
  25.    private   static   final  String URL =  "jdbc:oracle:thin:@10.11.25.66:1521:wjc" ;   
  26.    
  27.    /**   
  28.     * 用户名   
  29.     */    
  30.    private   static   final  String USER =  "wjc" ;   
  31.    
  32.    /**   
  33.     * 密码   
  34.     */    
  35.    private   static   final  String PASSWORD =  "anyue" ;   
  36.    
  37.    /**   
  38.     * 数据库连接   
  39.     */    
  40.    private   static  Connection conn =  null ;   
  41.    
  42.    /**   
  43.     * SQL语句对象   
  44.     */    
  45.    private   static  Statement stmt =  null ;   
  46.    
  47.    /**   
  48.     * @roseuid 3EDA089E02BC   
  49.     */    
  50.    public  LobPros()   
  51.    {   
  52.    
  53.    }   
  54.    
  55.    /**   
  56.     * 往数据库中插入一个新的CLOB对象   
  57.     *   
  58.     * @param infile - 数据文件   
  59.     * @throws java.lang.Exception   
  60.     * @roseuid 3EDA04A902BC   
  61.     */    
  62.    public   static   void  clobInsert(String infile)  throws  Exception   
  63.    {   
  64.        /* 设定不自动提交 */    
  65.        boolean  defaultCommit = conn.getAutoCommit();   
  66.        conn.setAutoCommit(false );   
  67.    
  68.        try  {   
  69.            /* 插入一个空的CLOB对象 */    
  70.            stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())" );   
  71.            /* 查询此CLOB对象并锁定 */    
  72.            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE" );   
  73.            while  (rs.next()) {   
  74.                /* 取出此CLOB对象 */    
  75.                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL" );   
  76.                /* 向CLOB对象中写入数据 */    
  77.                BufferedWriter out = new  BufferedWriter(clob.getCharacterOutputStream());   
  78.                BufferedReader in = new  BufferedReader( new  FileReader(infile));   
  79.                int  c;   
  80.                while  ((c=in.read())!=- 1 ) {   
  81.                    out.write(c);   
  82.                }   
  83.                in.close();   
  84.                out.close();   
  85.            }   
  86.            /* 正式提交 */    
  87.            conn.commit();   
  88.        } catch  (Exception ex) {   
  89.            /* 出错回滚 */    
  90.            conn.rollback();   
  91.            throw  ex;   
  92.        }   
  93.    
  94.        /* 恢复原提交状态 */    
  95.        conn.setAutoCommit(defaultCommit);   
  96.    }   
  97.    
  98.    /**   
  99.     * 修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)   
  100.     *   
  101.     * @param infile - 数据文件   
  102.     * @throws java.lang.Exception   
  103.     * @roseuid 3EDA04B60367   
  104.     */    
  105.    public   static   void  clobModify(String infile)  throws  Exception   
  106.    {   
  107.        /* 设定不自动提交 */    
  108.        boolean  defaultCommit = conn.getAutoCommit();   
  109.        conn.setAutoCommit(false );   
  110.    
  111.        try  {   
  112.            /* 查询CLOB对象并锁定 */    
  113.            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE" );   
  114.            while  (rs.next()) {   
  115.                /* 获取此CLOB对象 */    
  116.                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL" );   
  117.                /* 进行覆盖式修改 */    
  118.                BufferedWriter out = new  BufferedWriter(clob.getCharacterOutputStream());   
  119.                BufferedReader in = new  BufferedReader( new  FileReader(infile));   
  120.                int  c;   
  121.                while  ((c=in.read())!=- 1 ) {   
  122.                    out.write(c);   
  123.                }   
  124.                in.close();   
  125.                out.close();   
  126.            }   
  127.            /* 正式提交 */    
  128.            conn.commit();   
  129.        } catch  (Exception ex) {   
  130.            /* 出错回滚 */    
  131.            conn.rollback();   
  132.            throw  ex;   
  133.        }   
  134.    
  135.        /* 恢复原提交状态 */    
  136.        conn.setAutoCommit(defaultCommit);   
  137.    }   
  138.    
  139.    /**   
  140.     * 替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象)   
  141.     *   
  142.     * @param infile - 数据文件   
  143.     * @throws java.lang.Exception   
  144.     * @roseuid 3EDA04BF01E1   
  145.     */    
  146.    public   static   void  clobReplace(String infile)  throws  Exception   
  147.    {   
  148.        /* 设定不自动提交 */    
  149.        boolean  defaultCommit = conn.getAutoCommit();   
  150.        conn.setAutoCommit(false );   
  151.    
  152.        try  {   
  153.            /* 清空原CLOB对象 */    
  154.            stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'" );   
  155.            /* 查询CLOB对象并锁定 */    
  156.            ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE" );   
  157.            while  (rs.next()) {   
  158.                /* 获取此CLOB对象 */    
  159.                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL" );   
  160.                /* 更新数据 */    
  161.                BufferedWriter out = new  BufferedWriter(clob.getCharacterOutputStream());   
  162.                BufferedReader in = new  BufferedReader( new  FileReader(infile));   
  163.                int  c;   
  164.                while  ((c=in.read())!=- 1 ) {   
  165.                    out.write(c);   
  166.                }   
  167.                in.close();   
  168.                out.close();   
  169.            }   
  170.            /* 正式提交 */    
  171.            conn.commit();   
  172.        } catch  (Exception ex) {   
  173.            /* 出错回滚 */    
  174.            conn.rollback();   
  175.            throw  ex;   
  176.        }   
  177.    
  178.        /* 恢复原提交状态 */    
  179.        conn.setAutoCommit(defaultCommit);   
  180.    }   
  181.    
  182.    /**   
  183.     * CLOB对象读取   
  184.     *   
  185.     * @param outfile - 输出文件名   
  186.     * @throws java.lang.Exception   
  187.     * @roseuid 3EDA04D80116   
  188.     */    
  189.    public   static   void  clobRead(String outfile)  throws  Exception   
  190.    {   
  191.        /* 设定不自动提交 */    
  192.        boolean  defaultCommit = conn.getAutoCommit();   
  193.        conn.setAutoCommit(false );   
  194.    
  195.        try  {   
  196.            /* 查询CLOB对象 */    
  197.            ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'" );   
  198.            while  (rs.next()) {   
  199.                /* 获取CLOB对象 */    
  200.                oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL" );   
  201.                /* 以字符形式输出 */    
  202.                BufferedReader in = new  BufferedReader(clob.getCharacterStream());   
  203.                BufferedWriter out = new  BufferedWriter( new  FileWriter(outfile));   
  204.                int  c;   
  205.                while  ((c=in.read())!=- 1 ) {   
  206.                    out.write(c);   
  207.                }   
  208.                out.close();   
  209.                in.close();   
  210.            }   
  211.        } catch  (Exception ex) {   
  212.            conn.rollback();   
  213.            throw  ex;   
  214.        }   
  215.    
  216.        /* 恢复原提交状态 */    
  217.        conn.setAutoCommit(defaultCommit);   
  218.    }   
  219.    
  220.    /**   
  221.     * 向数据库中插入一个新的BLOB对象   
  222.     *   
  223.     * @param infile - 数据文件   
  224.     * @throws java.lang.Exception   
  225.     * @roseuid 3EDA04E300F6   
  226.     */    
  227.    public   static   void  blobInsert(String infile)  throws  Exception   
  228.    {   
  229.        /* 设定不自动提交 */    
  230.        boolean  defaultCommit = conn.getAutoCommit();   
  231.        conn.setAutoCommit(false );   
  232.    
  233.        try  {   
  234.            /* 插入一个空的BLOB对象 */    
  235.            stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())" );   
  236.            /* 查询此BLOB对象并锁定 */    
  237.            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE" );   
  238.            while  (rs.next()) {   
  239.                /* 取出此BLOB对象 */    
  240.                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL" );   
  241.                /* 向BLOB对象中写入数据 */    
  242.                BufferedOutputStream out = new  BufferedOutputStream(blob.getBinaryOutputStream());   
  243.                BufferedInputStream in = new  BufferedInputStream( new  FileInputStream(infile));   
  244.                int  c;   
  245.                while  ((c=in.read())!=- 1 ) {   
  246.                    out.write(c);   
  247.                }   
  248.                in.close();   
  249.                out.close();   
  250.            }   
  251.            /* 正式提交 */    
  252.            conn.commit();   
  253.        } catch  (Exception ex) {   
  254.            /* 出错回滚 */    
  255.            conn.rollback();   
  256.            throw  ex;   
  257.        }   
  258.    
  259.        /* 恢复原提交状态 */    
  260.        conn.setAutoCommit(defaultCommit);   
  261.    }   
  262.    
  263.    /**   
  264.     * 修改BLOB对象(是在原BLOB对象基础上进行覆盖式的修改)   
  265.     *   
  266.     * @param infile - 数据文件   
  267.     * @throws java.lang.Exception   
  268.     * @roseuid 3EDA04E90106   
  269.     */    
  270.    public   static   void  blobModify(String infile)  throws  Exception   
  271.    {   
  272.        /* 设定不自动提交 */    
  273.        boolean  defaultCommit = conn.getAutoCommit();   
  274.        conn.setAutoCommit(false );   
  275.    
  276.        try  {   
  277.            /* 查询BLOB对象并锁定 */    
  278.            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE" );   
  279.            while  (rs.next()) {   
  280.                /* 取出此BLOB对象 */    
  281.                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL" );   
  282.                /* 向BLOB对象中写入数据 */    
  283.                BufferedOutputStream out = new  BufferedOutputStream(blob.getBinaryOutputStream());   
  284.                BufferedInputStream in = new  BufferedInputStream( new  FileInputStream(infile));   
  285.                int  c;   
  286.                while  ((c=in.read())!=- 1 ) {   
  287.                    out.write(c);   
  288.                }   
  289.                in.close();   
  290.                out.close();   
  291.            }   
  292.            /* 正式提交 */    
  293.            conn.commit();   
  294.        } catch  (Exception ex) {   
  295.            /* 出错回滚 */    
  296.            conn.rollback();   
  297.            throw  ex;   
  298.        }   
  299.    
  300.        /* 恢复原提交状态 */    
  301.        conn.setAutoCommit(defaultCommit);   
  302.    }   
  303.    
  304.    /**   
  305.     * 替换BLOB对象(将原BLOB对象清除,换成一个全新的BLOB对象)   
  306.     *   
  307.     * @param infile - 数据文件   
  308.     * @throws java.lang.Exception   
  309.     * @roseuid 3EDA0505000C   
  310.     */    
  311.    public   static   void  blobReplace(String infile)  throws  Exception   
  312.    {   
  313.        /* 设定不自动提交 */    
  314.        boolean  defaultCommit = conn.getAutoCommit();   
  315.        conn.setAutoCommit(false );   
  316.    
  317.        try  {   
  318.            /* 清空原BLOB对象 */    
  319.            stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'" );   
  320.            /* 查询此BLOB对象并锁定 */    
  321.            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE" );   
  322.            while  (rs.next()) {   
  323.                /* 取出此BLOB对象 */    
  324.                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL" );   
  325.                /* 向BLOB对象中写入数据 */    
  326.                BufferedOutputStream out = new  BufferedOutputStream(blob.getBinaryOutputStream());   
  327.                BufferedInputStream in = new  BufferedInputStream( new  FileInputStream(infile));   
  328.                int  c;   
  329.                while  ((c=in.read())!=- 1 ) {   
  330.                    out.write(c);   
  331.                }   
  332.                in.close();   
  333.                out.close();   
  334.            }   
  335.            /* 正式提交 */    
  336.            conn.commit();   
  337.        } catch  (Exception ex) {   
  338.            /* 出错回滚 */    
  339.            conn.rollback();   
  340.            throw  ex;   
  341.        }   
  342.    
  343.        /* 恢复原提交状态 */    
  344.        conn.setAutoCommit(defaultCommit);   
  345.    }   
  346.    
  347.    /**   
  348.     * BLOB对象读取   
  349.     *   
  350.     * @param outfile - 输出文件名   
  351.     * @throws java.lang.Exception   
  352.     * @roseuid 3EDA050B003B   
  353.     */    
  354.    public   static   void  blobRead(String outfile)  throws  Exception   
  355.    {   
  356.        /* 设定不自动提交 */    
  357.        boolean  defaultCommit = conn.getAutoCommit();   
  358.        conn.setAutoCommit(false );   
  359.    
  360.        try  {   
  361.            /* 查询BLOB对象 */    
  362.            ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'" );   
  363.            while  (rs.next()) {   
  364.                /* 取出此BLOB对象 */    
  365.                oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL" );   
  366.                /* 以二进制形式输出 */    
  367.                BufferedOutputStream out = new  BufferedOutputStream( new  FileOutputStream(outfile));   
  368.                BufferedInputStream in = new  BufferedInputStream(blob.getBinaryStream());   
  369.                int  c;   
  370.                while  ((c=in.read())!=- 1 ) {   
  371.                    out.write(c);   
  372.                }   
  373.                in.close();   
  374.                out.close();   
  375.            }   
  376.            /* 正式提交 */    
  377.            conn.commit();   
  378.        } catch  (Exception ex) {   
  379.            /* 出错回滚 */    
  380.            conn.rollback();   
  381.            throw  ex;   
  382.        }   
  383.    
  384.        /* 恢复原提交状态 */    
  385.        conn.setAutoCommit(defaultCommit);   
  386.    }   
  387.    
  388.    /**   
  389.     * 建立测试用表格   
  390.     * @throws Exception   
  391.     */    
  392.    public   static   void  createTables()  throws  Exception {   
  393.        try  {   
  394.            stmt.executeUpdate("CREATE TABLE TEST_CLOB ( ID NUMBER(3), CLOBCOL CLOB)" );   
  395.            stmt.executeUpdate("CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)" );   
  396.        } catch  (Exception ex) {   
  397.    
  398.        }   
  399.    }   
  400.    
  401.    /**   
  402.     * @param args - 命令行参数   
  403.     * @throws java.lang.Exception   
  404.     * @roseuid 3EDA052002AC   
  405.     */    
  406.    public   static   void  main(String[] args)  throws  Exception   
  407.    {   
  408.        /* 装载驱动,建立数据库连接 */    
  409.        Class.forName(DRIVER);   
  410.        conn = DriverManager.getConnection(URL,USER,PASSWORD);   
  411.        stmt = conn.createStatement();   
  412.    
  413.        /* 建立测试表格 */    
  414.        createTables();   
  415.    
  416.        /* CLOB对象插入测试 */    
  417.        clobInsert("c:/clobInsert.txt" );   
  418.        clobRead("c:/clobInsert.out" );   
  419.    
  420.        /* CLOB对象修改测试 */    
  421.        clobModify("c:/clobModify.txt" );   
  422.        clobRead("c:/clobModify.out" );   
  423.    
  424.        /* CLOB对象替换测试 */    
  425.        clobReplace("c:/clobReplace.txt" );    </
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值