Weblogic 下 oracle 的 blob 操作

Weblogic 下 oracle 的 blob 操作

最后公司的WEB服务器要用 WebLogic 8.1 替换原来的 Resin ,因为要用webloic 的 数据库连接池,原有的程序需要变动
(1)向  blob 字段写入值
   
                       String medpk = dao.getElementPK(cn, vo);
                            evo.setEle_elementId(medpk);
                            evo.setEle_contentid(conpk);
                            evo.setEle_type("23");
                            dao.addElementRelation(cn, evo);
                            evo.setMmsnews_elementID(medpk);
                   //Connection conn=JdbcConnectionPool.getConnection();
                            try {   
                              String sql =
"insert into OM_CM_MMSNEWS (elementid,title,content) values(?,?,empty_blob())";                                                 //1.blob字段插入空值
                                PreparedStatement ps = cn.prepareStatement(sql);
                                 ps.setString(1, evo.getEle_elementId());
                                ps.setString(2, evo.getMmsnews_title());
                                ps.execute();
                                sql =
  "select content from OM_CM_MMSNEWS where elementid=" + medpk + " for update";                                   //2.更新指定记录的 blob字段
                                Statement st = cn.createStatement();
                                ResultSet rs = st.executeQuery(sql);
                                File file = new File(evo.getMmsnews_content());
                                if (rs.next()) {
   OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob(1);       
                                                                //必须用weblogic 的jar包
                                    OutputStream outStream = blob. getBinaryOutputStream();
                                    InputStream fin = new FileInputStream(file);
                                    byte[] b = new byte[blob.getBufferSize()];
                                    int len = 0;
                                    while ((len = fin.read(b)) != -1) {
                                        outStream.write(b, 0, len);
                                    }
                                    fin.close();
                                    outStream.flush();
                                    outStream.close();
                                }
                                rs.close();
                                st.close();
                                logger.error("this MmsNews is save!");
                            } catch (Exception ex) {
                                logger.error(ex.getMessage());
                                throw new Exception(ex.getMessage());
                            }



(2)提取blob字段的值,并保存为文件
 private static void getZipFile(String contentid, String filepath,
                                   String zipfile) throws
            DOException {

        String sql = "select  content  from om_cm_mmsnews a where a.elementid in  (select b.elementid from om_cm_elementrelation b where b.contentid in (select c.contentid from om_cm_content c where c.contentid=?))";
        Connection conn = null;
        try {
            //建立文件夹
            File tempDicFile = new java.io.File(filepath);
            tempDicFile.mkdirs();

            //提取 blob 字段的内容并保存为 zip 文件
            conn = JdbcConnectionPool.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, contentid); //传入 contentid
            ResultSet rst = ps.executeQuery();
            while (rst.next()) {
                /* 取出此BLOB对象 */
                java.sql.Blob blob = rst.getBlob("content");
                if (blob != null) {
                    /* 以二进制形式输出 */
                    BufferedInputStream in = new BufferedInputStream(blob.
                            getBinaryStream());
                    BufferedOutputStream out = new BufferedOutputStream(new
                            FileOutputStream(zipfile));
                    byte[] buf = new byte[1024];
                    while (true)
                    {
                        int count = in.read(buf);
                        if (count <= 0)
                        {
                            break;
                        }
                        out.write(buf, 0, count);
                    }
                    in.close();
                    out.close();
                }
            }
            rst.close();
            ps.close();
            conn.close();
            logger.error("Mmspreview -->getZipFile 创建zip文件成功");
        } catch (Exception ex) {
            logger.error("Mmspreview -->getZipFile 创建zip文件失败!!!!!" +
                         ex.getMessage());
        } finally {
            PublicUtilit.rapidReleaseConnection(null, conn, null, null);
        }
    }

你可能感兴趣的:(Weblogic 下 oracle 的 blob 操作)