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 + "]";
} }

存储测试代码

@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[]; int len = ;
while((len = inputStream.read(bytes))!=-){
baos.write(bytes, , 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类似

BLOB和CLOB的更多相关文章

  1. oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  2. Oracle中Blob和Clob类型的区别与操作

    Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...

  3. 操作BLOB、CLOB、BFILE

    BFILE        二进制文件,存储在数据库外的操作系统文件,只读的.把此文件当二进制处理. BLOB        二进制大对象.存储在数据库里的大对象,一般是图像声音等文件. CLOB    ...

  4. BLOB TO CLOB

    CREATE OR REPLACE FUNCTION blob_to_clob (blob_in IN BLOB) RETURN CLOB AS v_clob CLOB; v_varchar VARC ...

  5. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  6. Blob和Clob在JDBC中的简介

    数据库在当今的应用越来越广泛了,同样伴随着领域的广泛,存储的内容也不在是只有数值.字符.boolean几种类型,而是越来越多样化.在这样的前提下就出现了Blob和Clob两个类型.下面我将对这个两个类 ...

  7. panzer 电力项目十一--hibernate操作大文本字段Blob和Clob

    hibernate操作大文本字段Blob和Clob解决方案: 1.大文本字段Blob和Clob(流); 2.截串存取 第一步: 创建新表:Elec_CommonMsg_Content create t ...

  8. Hibernate保存Blob和Clob类型的数据

    虽然非常不建议在数据库中保存Blob和Clob类型的数据,但真的要有这样的需求呢?这里记录一下使用Hibernate如何向数据库中保存Blob和Clob数据. Oracle和MySql在Blob类型上 ...

  9. 问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  10. Spring 让 LOB 数据操作变得简单易行,LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型

    转自:https://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/index.html 概述 LOB 代表大对象数据,包括 BLOB 和 CL ...

随机推荐

  1. sentinel.conf样例

    #禁止用localhost与127.0.0.1# Example sentinel.conf # 哨兵sentinel实例运行的端口 默认26379port 26379 # 哨兵sentinel的工作 ...

  2. Android jni/ndk编程三:native访问java

    一.访问静态字段 Java层的field和method,不管它是public,还是package.private和protected,从 JNI都可以访问到,Java面向语言的封装性不见了. 静态字段 ...

  3. JS字符串转换为JSON的方法

    1.jQuery插件支持的转换方式:  示例: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 2.浏览 ...

  4. 算法 - 插入排序交换次数 - Binary Indexed Tree

    场景:快速得到一段数组元素的和 题目:Insertion Sort Advanced Analysis | HackerRank 算法:binary-indexed-tree :: HackerRan ...

  5. 七十六:flask.Restful之flask-Restful插件的基本使用

    安装:flask 0.8以上.python2.6或者3.3以上:pip install flask-restful 使用方法:1.从flask_restful中导入Api,来创建对象 2.写一个视图函 ...

  6. HTTP Status 500 – Internal Server Error

    明明硬盘中存在users.xml,但是提示:系统找不到指定的路径. 解决办法: 设置String的编码格式 realpath = URLDecoder.decode(realpath, "U ...

  7. systemctl stop 列表

    root@devstack2019:/etc/keystone# systemctl stop Display all 201 possibilities? (y or n)accounts-daem ...

  8. 并发编程之原子Atomic&Unsafe

    1.原子更新基本类型类   用于通过原子的方式更新基本类型,Atomic包提供了以下三个类: AtomicBoolean:原子更新布尔类型. AtomicInteger:原子更新整型. AtomicL ...

  9. ios 后台进程弹窗

    // http://iphonedevwiki.net/index.php/CFUserNotification // https://kunnan.github.io/2018/05/14/com. ...

  10. 微擎-T

    微擎菜单栏对应的数据库表 ims_modules_bindings 小程序前端uniacid的配置,微擎后台进入小程序应用时鼠标移动至管理查看链接即可(不点击) ims_account_wxapp微擎 ...