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. vue文件流转换成pdf预览(pdf.js+iframe)

    参考文档:https://www.jianshu.com/p/242525315bf6 PDFJS: https://mozilla.github.io/pdf.js/     支持获取文件流到客户端 ...

  2. javascript之Prototype属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. flutter textfield设置高度后内容区无法居中?

    textfiled 设置高度后,内容永远无法居中,最后找到原因 decoration: 中有一个 contentPadding属性,设置这个属性对应的Padding值即可

  4. (转)java8实现对象列表去重

    java8实现列表去重,java8的stream和lambda的使用实例 通过普通的方式也可以达到去重的效果,但是借助java8新特性可以很方便的实现列表去重,测试demo如下 实体类: public ...

  5. LoadRunner中的Web 函数列表

    LoadRunner中的Web 函数列表 web test LoadRunner fuction_list D:\Program Files (x86)\Mercury Interactive\Mer ...

  6. 集群中配置多台机器之间 SSH 免密码登录

    集群中配置多台机器之间 SSH 免密码登录 问题描述 由于现在项目大多数由传统的单台机器部署,慢慢转变成多机器的集群化部署. 但是,这就涉及到机器间的 SSH 免密码互通问题. 当集群机器比较多的时候 ...

  7. springboot jar启动 读取jar包中相对路径文件报错

    jar包启动后读取相对路径文件报异常: Caused by: java.io.FileNotFoundException: class path resource [***.***] cannot b ...

  8. java线程中start和run的区别

    public class Test1 extends Thread { @Override public void run() { while (true) { System.out.println( ...

  9. eclipse 导入项目出现的问题汇总

    Unbound classpath variable: 'TOMCAT_HOME' in project XXX Problem 翻译:未绑定的类路径变量:项目XXX中的“TOMCAT_HOME” 解 ...

  10. VS2008新增文件没有模板

    可能是我安装的过程中发神经没有选中选项什么的,打开来想建个项目发现一个模板都没有,那就很尴尬了,作为对开发工具极度依赖的人,这真的难受... 在网上找到别人的办法 开始 –> 程序 –> ...