zoukankan      html  css  js  c++  java
  • BLOB和CLOB

    mysql各数据类型及字节长度一览表:

    数据类型 字节长度 范围或用法
    Bit 1 无符号[0,255],有符号[-128,127],天缘博客备注:BIT和BOOL布尔型都占用1字节
    TinyInt 1 整数[0,255]
    SmallInt 2 无符号[0,65535],有符号[-32768,32767]
    MediumInt 3 无符号[0,2^24-1],有符号[-2^23,2^23-1]]
    Int 4 无符号[0,2^32-1],有符号[-2^31,2^31-1]
    BigInt 8 无符号[0,2^64-1],有符号[-2^63 ,2^63 -1]
    Float(M,D) 4 单精度浮点数。天缘博客提醒这里的D是精度,如果D<=24则为默认的FLOAT,如果D>24则会自动被转换为DOUBLE型。
    Double(M,D) 8  双精度浮点。
    Decimal(M,D) M+1或M+2 未打包的浮点数,用法类似于FLOAT和DOUBLE,天缘博客提醒您如果在ASP中使用到Decimal数据类型,直接从数据库读出来的Decimal可能需要先转换成Float或Double类型后再进行运算。
    Date 3 以YYYY-MM-DD的格式显示,比如:2009-07-19
    Date Time 8 以YYYY-MM-DD HH:MM:SS的格式显示,比如:2009-07-19 11:22:30
    TimeStamp 4 以YYYY-MM-DD的格式显示,比如:2009-07-19
    Time 3 以HH:MM:SS的格式显示。比如:11:22:30
    Year 1 以YYYY的格式显示。比如:2009
    Char(M) M 定长字符串。
    VarChar(M) M 变长字符串,要求M<=255
    Binary(M) M 类似Char的二进制存储,特点是插入定长不足补0
    VarBinary(M) M 类似VarChar的变长二进制存储,特点是定长不补0
    Tiny Text Max:255 大小写不敏感
    Text Max:64K 大小写不敏感
    Medium Text Max:16M 大小写不敏感
    Long Text Max:4G 大小写不敏感
    TinyBlob Max:255 大小写敏感
    Blob Max:64K 大小写敏感
    MediumBlob Max:16M 大小写敏感
    LongBlob Max:4G 大小写敏感
    Enum 1或2 最大可达65535个不同的枚举值
    Set 可达8 最大可达64个不同的值
    Geometry    
    Point    
    LineString    
    Polygon    
    MultiPoint    
    MultiLineString    
    MultiPolygon    
    GeometryCollection    

    一.BLOB存储(hibernate4)
    实体类

    package com.my.dm.model;
    
    import java.sql.Blob;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="image")
    public class ImgFile {
        
        @Id
        @Column(name="IMG_ID")
        private String imgId;
        
        @Column(name="IMG_NAME")
        private String imgName;
        
        @Column(name="IMG_SIZE")
        private double imgSize;
        
        
        @Column(name="IMG_CONTENT")
        private Blob imgContent;
    
    
    
        /**
         * @return the imgId
         */
        public String getImgId() {
            return imgId;
        }
    
    
        /**
         * @param imgId the imgId to set
         */
        public void setImgId(String imgId) {
            this.imgId = imgId;
        }
    
    
        /**
         * @return the imgName
         */
        public String getImgName() {
            return imgName;
        }
    
    
        /**
         * @param imgName the imgName to set
         */
        public void setImgName(String imgName) {
            this.imgName = imgName;
        }
    
    
        /**
         * @return the imgSize
         */
        public double getImgSize() {
            return imgSize;
        }
    
    
        /**
         * @param imgSize the imgSize to set
         */
        public void setImgSize(double imgSize) {
            this.imgSize = imgSize;
        }
    
    
        /**
         * @return the imgContent
         */
        public Blob getImgContent() {
            return imgContent;
        }
    
    
        /**
         * @param imgContent the imgContent to set
         */
        public void setImgContent(Blob imgContent) {
            this.imgContent = imgContent;
        }
    
    
        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "ImgFile [imgId=" + imgId + ", imgName=" + imgName + ", imgSize=" + imgSize + ", imgContent="
                    + imgContent + "]";
        }
        
    
    }
    View Code

    存储测试代码

    @Override
        public void saveImage() {
            // TODO Auto-generated method stub
            Session session = sessionFactory.getCurrentSession();
            
            ImgFile img = new ImgFile();
            
            img.setImgId(UUID.randomUUID().toString().replace("-", ""));
            img.setImgName("testImg");
            img.setImgSize(1024.00);
            File file = new File("D:\test\pic\Koala.jpg");
            try {
                FileInputStream inputStream = new FileInputStream(file);
                Blob blob = Hibernate.getLobCreator(session).createBlob(inputStream, file.length());//(inputStream, inputStream.available())
            // 也可用 session.getLobHelper().createBlob(inputStream, inputStream.available()); img.setImgContent(blob); session.save(img); }
    catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

    可以用byte[] 创建blob

    FileInputStream inputStream = new FileInputStream(file);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                
                int len = 0 ;
                while((len = inputStream.read(bytes))!=-1){
                    baos.write(bytes, 0, len);
                }
                
                byte[] inByte = baos.toByteArray();
                
                Blob blob = Hibernate.getLobCreator(session).createBlob(inByte);

     hibernate3中为:

      InputStream in = new FileInputStream("F:\4563123.jpg");
            Blob blob = Hibernate.createBlob(in);
            //得到简介的clob
            Clob clob = Hibernate.createClob("这是一本书和详细描述。#(*&#@¥%(*&@¥)(@#¥#¥");

     二.BLOB的读取

    @Override
        public ImgFile getImageById(String imgId) {
            // TODO Auto-generated method stub
            Session session = sessionFactory.getCurrentSession();
            ImgFile imgFile = (ImgFile) session.get(ImgFile.class, imgId);
            Blob imgcontent = imgFile.getImgContent();
            OutputStream out = null;
            InputStream in = null;
            byte[] bs = new byte[1024];
            int len = 0;
            try {
                out = new FileOutputStream(new File("D:\test\out\device.txt"));
           //byte[] bs= imgcontent.getBytes(1, (int)imgcontent.length()); in
    = imgcontent.getBinaryStream(); while ((len = in.read(bs))!=-1) { out.write(bs, 0, len); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return imgFile; }

    三.CLOB的存储(mysql 没有clob。只有text,longtext,操作方法和varchar一样当作String处理

    处理CLOB和BLOB类似

  • 相关阅读:
    e621. Activating a Keystroke When Any Child Component Has Focus
    e587. Filling Basic Shapes
    e591. Drawing Simple Text
    e595. Drawing an Image
    e586. Drawing Simple Shapes
    e636. Listening to All Key Events Before Delivery to Focused Component
    在 PL/SQL 块的哪部分可以对初始变量赋予新值? (选择1项)
    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
    你判断下面语句,有什么作用?(单选)
    Oracle数据库表空间与数据文件的关系描述正确的是( )
  • 原文地址:https://www.cnblogs.com/lukelook/p/11067705.html
Copyright © 2011-2022 走看看