关于Blob类型字段的存储和获取

来源:互联网 发布:二级域名与一级域名 编辑:程序博客网 时间:2024/06/05 00:49

1、存储(LAWCONTENT字段为BLOB类型)

public boolean ExecuteAddLaw(Law law) throws Exception {
        boolean nResult = false;
        StringBuilder sqlInsert = new StringBuilder();
        StringBuilder sqlSelect = new StringBuilder();
        sqlInsert.append(" INSERT INTO T04_LAW (LAWID, LAWTITLE, LAWBELONG, LAWCONTENT, DELFLAG, CREATEUSERID, CREATEDATE) ");
        sqlInsert.append(" VALUES ('").append(law.getLawId()).append("', ");
        sqlInsert.append(" '").append(law.getLawTitle()).append("', ");
        sqlInsert.append(" '").append(law.getLawBelong()).append("', ");
        sqlInsert.append("empty_blob(), ");
        sqlInsert.append(" '").append(law.getDelFlag()).append("', ");
        sqlInsert.append(" '").append(law.getCreateUserId()).append("', ");
        sqlInsert.append(" to_date('").append(CommonUtil.GetCurrentDate()).append("', 'yyyy-MM-dd hh24:mi:ss')) ");
        sqlSelect.append("SELECT LAWCONTENT FROM  T04_LAW  WHERE  LAWID='").append(law.getLawId()).append("' FOR UPDATE");
        try {
            connection.setAutoCommit(false);
            Statement st = connection.createStatement();
            st.executeUpdate(sqlInsert.toString());
            ResultSet rs = st.executeQuery(sqlSelect.toString());
            if (rs.next()) {
                String content = law.getLawContent();
                BLOB blob = (BLOB) rs.getBlob(1);
                OutputStream out = blob.getBinaryOutputStream();
                InputStream in = new ByteArrayInputStream(content.getBytes("GBK"));   
                byte[] b = new byte[blob.getBufferSize()];
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                in.close();
                out.flush();
                out.close();
                CommitTransaction();
                st.close();
                rs.close();
                connection.close();
                nResult = true;
            }
        }catch (Exception e) {
            RollbackTransaction();
            e.printStackTrace();
        }
        return nResult;
    }

2、读取

public Law updatelaw(String lawId) throws Exception {
        IDbOperator dbop = DBOperator.getInstance();
        List<DbSqlParameter> sqlparam = new ArrayList<DbSqlParameter>();
        StringBuilder sql = new StringBuilder();
        ResultSet rs = null;
        Law law = new Law();
        sql.append(" SELECT LAWID, LAWTITLE, LAWBELONG, LAWCONTENT FROM T04_LAW ");
        sql.append(" WHERE DELFLAG = 0 ");
        sql.append(" AND LAWID = ? ");
        sqlparam.add(new DbSqlParameter("LAWID", lawId.split(",")[0]));
        try {
            rs = dbop.ExecuteQueryBLOB(sql.toString(), sqlparam);
            if (rs != null && rs.next()) {
                law.setLawId(rs.getString("LAWID"));
                law.setLawTitle(rs.getString("LAWTITLE"));
                law.setLawBelong(rs.getString("LAWBELONG"));
                law.setLawContent(getBLODTransform(rs.getBlob("LAWCONTENT")));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return law;
    }

    /**
     * 对BLOB数据类型String的转换
     *
     * @param Oracle.sql中BLOB类型 ResultSet.getBLOB方法获得
     * @return String 字符串
     * @throws Exception
     */
    public static String getBLODTransform(Blob b) throws Exception {
        InputStream stream = b.getBinaryStream();
        Reader read = new InputStreamReader(stream, "GBK");
        char[] ch = new char[(int) b.length()];
        read.read(ch);
        String result = new String(ch);
        return result.trim();
    }




原创粉丝点击