在写java程序过程中,如何读取Oracle数据库表某类型为blob的字段?

以下是我在写程序的时候一种解决方法。核心语句。(传上来做了修改,格式不要学习,养成良好习惯)

详细请参考:

读取序列ID:http://blog.csdn.net/yzsind/article/details/6918506

BLOB相关:http://jslfl.iteye.com/blog/1771949

http://www.linuxidc.com/Linux/2011-08/40218.htm

http://xwwccsucn.blog.163.com/blog/static/979145792011925234392/


1. 读取BLOB字段

import oracle.sql.BLOB;

ResultSet rs = ps.executeQuery(sql);

BLOB blob = (BLOB) rs.getBlob("content");

String s = blobToString(blob);


2. blobToString()方法

private String blobToString(BLOB blob) throws SQLException, UnsupportedEncodingException {

        long BlobLength; // BLOB字段长度

        int i = 1; // 循环变量

        byte[] bytes; // BLOB临时存储字节数组

        String newStr = ""; // 返回字符串


        byte[] msgContent = blob.getBytes(); // BLOB转换为字节数组

        BlobLength = blob.length();  //获取BLOB长度  

        if (msgContent == null || BlobLength == 0) //如果为空,返回空值  

        {

            return "";

        } else {

            while (i <= BlobLength) //循环处理字符串转换,每次1024;Oracle字符串限制最大4k 

            {

                bytes = blob.getBytes(i, 1024);

                i = i + 1024;

                newStr = newStr + new String(bytes);

            }

        }

        return newStr;

    }

3. update BLOB字段

    public void insertData(String task_name, String tast_target) {

        int num = 0;

        String hql = "insert into table values(sequence_id.nextval,?,EMPTY_BLOB()) returning id into ?";


        try {

            Connection conn = GlobalData.getConnection();

            OraclePreparedStatement ops = (OraclePreparedStatement) conn.prepareStatement(hql);

            ops.setString(1, task_name);

            ops.registerReturnParameter(2, OracleTypes.NUMBER);


            num = ops.executeUpdate();

            ResultSet rset = ops.getReturnResultSet(); // rest is not null

            long id = 0;

            while (rset.next()) {

                id = rset.getLong(1);

                System.out.println("Insert returnning: " + id);

            }


            //更新blob字段

            Statement bst = conn.createStatement();

            ResultSet bset = bst.executeQuery("select tast_target from table  where id=" + id + " FOR UPDATE");


            BLOB blob = null;

            while (bset.next()) {

                blob = ((OracleResultSet) bset).getBLOB(1);

            }

            final java.io.BufferedOutputStream out = new java.io.BufferedOutputStream(blob.getBinaryOutputStream());//输出流

            ByteArrayInputStream streamTemp = null;//输入流

            try {

                streamTemp = new ByteArrayInputStream(tast_target.getBytes("ISO-8859-1"));

            


} catch (UnsupportedEncodingException ex) {

                Logger.getLogger(OracleGenelDaoImpl.class  


.getName()).log(Level.SEVERE, null, ex);

            }


            byte[] buffer = new byte[1024];//缓冲区


            int length = -1;

            try {

                while ((length = streamTemp.read(buffer)) != -1) {

                    out.write(buffer, 0, length);

                }

                streamTemp.close();

                out.close();

            


} catch (IOException ex) {

                Logger.getLogger(OracleGenelDaoImpl.class  


.getName()).log(Level.SEVERE, null, ex);

            }

            conn.commit();

            ops.close();

            bst.close();

        


} catch (SQLException ex) {

            Logger.getLogger(OracleGenelDaoImpl.class  


.getName()).log(Level.SEVERE, null, ex);

        }

    }