• Blob与Clob转字符串


    /**
         * blob转字符串
         * 
         * @param blob
         * @return
         * @throws IOException
         * @throws SQLException
         */
        public static String blobToString(Blob blob) throws IOException, SQLException {
            InputStream inputStream = blob.getBinaryStream();
            byte[] bytes = null;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bufferBytes = new byte[1024];
            int readLength = 0;
            while ((readLength = inputStream.read(bufferBytes, 0, bufferBytes.length)) > 0) {
                byteArrayOutputStream.write(bufferBytes, 0, readLength);
            }
            bytes = byteArrayOutputStream.toByteArray();
            String str = new String(bytes, "gbk");
            return str;
        }
    
        /**
         * clob转字符串
         * 
         * @param clob
         * @return
         * @throws SQLException
         * @throws IOException
         */
        public static String clobToString(Clob clob) throws SQLException, IOException {
    
            String reString = "";
            Reader is = clob.getCharacterStream();// 得到流
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s);
                s = br.readLine();
            }
            reString = sb.toString();
            return reString;
        }
    
        /**
         * 获取clob或blob字段内容
         * 
         * @param resultSet            结果集
         * @param clobOrBlobColumnName 字段名
         * @return
         * @throws SQLException
         * @throws UnsupportedEncodingException
         * @throws IOException
         */
        public static String getClobOrBlobContent(ResultSet resultSet, String clobOrBlobColumnName)
                throws SQLException, UnsupportedEncodingException, IOException {
            String str = "";
            String type = "";
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                if (resultSet.getMetaData().getColumnName(i).equals(clobOrBlobColumnName)) {
                    type = resultSet.getMetaData().getColumnTypeName(i);
                    break;
                }
            }
            if (type == "BLOB") {
                Blob blob = resultSet.getBlob(clobOrBlobColumnName);
                if (blob != null)
                    str = blobToString(blob);// 获取数据库blob字段
            }
            if (type == "CLOB") {
                Clob clob = resultSet.getClob(clobOrBlobColumnName);
                if (clob != null)
                    str = clobToString(clob);
            }
            return str;
    
        }
  • 相关阅读:
    css命名书写规范小结。
    不断学习,充实自己。
    【openGL】画正弦函数图像
    【openGL】画五角星
    【openGL】画圆
    【openGL】画直线
    【网络资料】Astar算法详解
    【Agorithm】一次一密加密解密算法
    【HTML5】 web上的音频
    【sicily】卡片游戏
  • 原文地址:https://www.cnblogs.com/ohmyuan/p/10784786.html
Copyright © 2020-2023  润新知