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 + "]";
} }
存储测试代码
@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的更多相关文章
- oracle中Blob和Clob类型的区别
BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...
- Oracle中Blob和Clob类型的区别与操作
Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...
- 操作BLOB、CLOB、BFILE
BFILE 二进制文件,存储在数据库外的操作系统文件,只读的.把此文件当二进制处理. BLOB 二进制大对象.存储在数据库里的大对象,一般是图像声音等文件. CLOB ...
- BLOB TO CLOB
CREATE OR REPLACE FUNCTION blob_to_clob (blob_in IN BLOB) RETURN CLOB AS v_clob CLOB; v_varchar VARC ...
- Hibernate or JPA Annotation中BLOB、CLOB注解写法
BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...
- Blob和Clob在JDBC中的简介
数据库在当今的应用越来越广泛了,同样伴随着领域的广泛,存储的内容也不在是只有数值.字符.boolean几种类型,而是越来越多样化.在这样的前提下就出现了Blob和Clob两个类型.下面我将对这个两个类 ...
- panzer 电力项目十一--hibernate操作大文本字段Blob和Clob
hibernate操作大文本字段Blob和Clob解决方案: 1.大文本字段Blob和Clob(流); 2.截串存取 第一步: 创建新表:Elec_CommonMsg_Content create t ...
- Hibernate保存Blob和Clob类型的数据
虽然非常不建议在数据库中保存Blob和Clob类型的数据,但真的要有这样的需求呢?这里记录一下使用Hibernate如何向数据库中保存Blob和Clob数据. Oracle和MySql在Blob类型上 ...
- 问题:oracle CLOB类型;结果:oracle中Blob和Clob类型的区别
BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...
- Spring 让 LOB 数据操作变得简单易行,LOB 代表大对象数据,包括 BLOB 和 CLOB 两种类型
转自:https://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/index.html 概述 LOB 代表大对象数据,包括 BLOB 和 CL ...
随机推荐
- ubuntu安装相关
安装系统参考:http://xinzhi.wenda.so.com/a/1523530837610141 首先进行更新源 sudo apt-get update 安装codeblocks sudo a ...
- vue 运用ElementUI,做select下拉框回显
第一.加载的顺序,应该先加载下拉框要选择的数据,然后在通过编辑查询数据后回显. 第二.要保证select下拉的ID和v-model里边的id保持一致. 第三.elementUI就会自动的将数据回显了. ...
- 【React自制全家桶】二、分析React的虚拟DOM和Diff算法
一.React如何更新DOM内容: 1. 获取state 数据 2. 获取JSX模版 3. 通过数据 +模版结合,生成真实的DOM, 来显示,以下行代码为例(简称代码1) <div id= ...
- linux卸载Python3
一.卸载Python3 1.卸载python3 rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps 卸载pyhton3 2.whereis ...
- Node.js的事件轮询Event Loop原理
Node.js的事件轮询Event Loop原理解释 事件轮询主要是针对事件队列进行轮询,事件生产者将事件排队放入队列中,队列另外一端有一个线程称为事件消费者会不断查询队列中是否有事件,如果有事件,就 ...
- axios的中文使用文档
axios 基于promise用于浏览器和node.js的http客户端 原文链接 lewis1990@amoy 特点 支持浏览器和node.js 支持promise 能拦截请求和响应 能转换请求和响 ...
- debian中安装gcc make
ubuntu debian 可以直接 apt-get install gcc automake autoconf libtool make
- golang(06)函数介绍
原文链接 http://www.limerence2017.com/2019/09/11/golang11/#more 函数简介 函数是编程语言中不可缺少的部分,在golang这门语言中函数是一等公民 ...
- git merge --squash 选项合并commit操作实例
参考: [转] git merge 将多个commit合并为一条之--squash 选项 git checkout master git pull origin master # 本地先拉取最新的m ...
- CENTOS7下安装和配置MYSQL问题记录
1.安装 下载mysql源安装包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm ...