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. Java快排

    package quickSort; /** * 快速排序 * @author root * */ public class QuickSort { static int[] data = {0,2, ...

  2. LC 966. Vowel Spellchecker

    Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...

  3. pandas之DataFrame创建、索引、切片等基础操作

    知识点 Series只有行索引,而DataFrame对象既有行索引,也有列索引 行索引,表明不同行,横向索引,叫index,0轴,axis=0 列索引,表明不同列,纵向索引,叫columns,1轴,a ...

  4. git重命名文件和文件夹

    git mv -f oldfolder newfoldergit add -u newfolder (-u选项会更新已经追踪的文件和文件夹)git commit -m "changed th ...

  5. tomcat启动不了?

    tomcat是用Java编写的,首先需要配置好jdk虚拟机, 要安装 JDK,请转至http://www.oracle.com/technetwork/java/javase/downloads/jd ...

  6. 基于nodeJS的小说爬虫实战

    背景与需求分析 最近迷恋于王者荣耀.斗鱼直播与B站吃播视频,中毒太深,下班之后无心看书. 为了摆脱现状,能习惯看书,我开始看小说了,然而小说网站广告多而烦,屌丝心态不愿充钱,于是想到了爬虫. 功能分析 ...

  7. php composer 开发自己的包

    以往都是在项目直接写自己的包文件,并没有把他放在packagist上面,以composer来管理使用. 今天没事来整一下,供大家一起学习 一,在github和packagist分别注册自己的账号,这里 ...

  8. 【MM系列】SAP技巧之更改布局

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP技巧之更改布局   前言部分 ...

  9. vscode配置PHP Debug

    1.先在vscode中安装PHP Debug,在设置添加“php.validate.executablePath”项,选中对应版本的php.exe. "php.validate.execut ...

  10. js继承的方式及其优缺点

    js继承方法 前因:ECMAScript不支持接口继承,只支持实现继承 一.原型链 概念:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针,让 ...