`

mysql - Clob Blob 转化 String

 
阅读更多

    String s1="1231dsdgasd的飒飒大";

         
        Clob c = new SerialClob(s1.toCharArray());//String 转 clob
        Blob b = new SerialBlob(s1.getBytes("GBK"));//String 转 blob
//      也可以这样不传字符集名称,默认使用系统的
//      Blob b = new SerialBlob(s1.getBytes());
         
        String clobString = c.getSubString(1, (int) c.length());//clob 转 String
        String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");//blob 转 String
//      前面若没传入字符集名称,则这里也不需要传入,以免出错
//      String blobString = new String(b.getBytes(1, (int) b.length()));
         
        System.out.println(clobString);
        System.out.println(blobString);
 
 
  /**
     * Blob类型 转 String类型
     * @param blob
     * @return
      *import java.sql.Blob;
     */
    public static String blobToString(Blob blob){
        String result = "";
        if ( blob != null ){
            InputStream is;
            try {
                is = blob.getBinaryStream();
                ByteArrayInputStream bais = (ByteArrayInputStream)is;
                byte[] byte_data = new byte[bais.available()];
                bais.read(byte_data,0,byte_data.length);
                result = new String(byte_data,"utf-8");
                is.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
分享到:
评论

相关推荐

    kettle通过java代码将数据库blob 字段抽取到本地文件

    kettle通过java代码将数据库blob 字段抽取到本地文件

    MYSQL,SQLSERVER,ORACLE常用的函数

    SQL> select instr('oracle traning','ra',1,2) instring from dual; INSTRING --------- 9 6.LENGTH 返回字符串的长度; SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from ....

    jdbc批量插入大字段

    针对oracle中blob字段的操作,能批量快速的插入大字段,效率非常高

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

    DevArt UniDAC v.3.70.0.18 (15-Jun-2011) (with Source Code)

    Support of BLOB, CLOB, and NCLOB data types in TUniLoader is improved PostgreSQL data provider Application Name connection option is supported Payload parameter for PostgreSQL notification is ...

    unidac_7_1_4_pro DELPHI 10 Tokyo

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    Unidac Pro 7.1.4 XE8

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    unidac_7_1_4_pro DELPHI 10 Berlin

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    UniDAC 7.1.4

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    DBKING使用指南

    在dbking中,所有的数据库数据只有五种数据类型,String、Number(BigDecimal)、Timestamp、Clob(String)、Blob(byte[]),经过反复测试后,我们会例出各种数据库数据类型到这五种类型的映射表,当然我们也有...

    SqliteDev 384

     g CLOB  h BLOB  i TIMESTAMP  j NUMERIC 10 5  k VARYING CHARACTER 24  l NATIONAL VARYING CHARACTER 16  ;  前面提到在某种情况下 SQLite的字段并不是无类型的 即在字段类型为”Integer Primary ...

    SQLite(SqliteDev)

     g CLOB,  h BLOB,  i TIMESTAMP,  j NUMERIC(10,5)  k VARYING CHARACTER (24),  l NATIONAL VARYING CHARACTER(16)  );  前面提到在某种情况下, SQLite的字段并不是无类型的. 即在字段类型为”Integer ...

    Doctrine ORM for PHP.pdf

    Table of Contents Introduction....................................................................................................13 Code Examples.........................................................

    Java开发详解.zip

    031708_【第17章:Java数据库编程】_处理大数据对象(1)—处理CLOB数据笔记.pdf 031709_【第17章:Java数据库编程】_处理大数据对象(2)—处理BLOB数据笔记.pdf 031710_【第17章:Java数据库编程】_...

Global site tag (gtag.js) - Google Analytics