Clob,Blob存储大对象的java操作

Clob: Character Large Object:大文本对象:比如一本几百万字的文本;
Blob:Binary Large Object: 二进制数据的大对象:比如图片,音频,视频!
大对象在数据库中以一列的值的形式存储,每一列存储一个完整的大对象,每一列只有一行!
// Clob支持的操作:

OutputStream setAsciiStream(long pos) // 设置字节流用于写

Retrieves a stream to be used to write Ascii characters to the CLOB value that this Clob object represents, starting at position pos.

Writer setCharacterStream(long pos) // 设置字符流用于写

Retrieves a stream to be used to write a stream of Unicode characters to the CLOB value that this Clob object represents, at position pos.

InputStream getAsciiStream() // 获取字节流用于读

Retrieves the CLOB value designated by this Clob object as an ascii stream.

Reader getCharacterStream() // 获取字符流用于读

Retrieves the CLOB value designated by this Clob object as a .io.Reader object (or as a stream of characters).

Reader getCharacterStream(long pos, long length) // 获取指定位置指定长度的字符流

Returns a Reader object that contains a partial Clob value, starting with the character specified by pos, which is length characters in length.

String getSubString(long pos, int length) // 读取一个定位、定长的字符串格式的数据

Retrieves a copy of the specified substring in the CLOB value designated by this Clob object.         
// Blob支持的操作:

OutputStream setBinaryStream(long pos) // 设置二进制流用于写

Retrieves a stream that can be used to write to the BLOB value that this Blob object represents.

int setBytes(long pos, byte[] bytes) // 设置内存流用于写

Writes the given array of bytes to the BLOB value that this Blob object represents, starting at position pos, and returns the number of bytes written.

int setBytes(long pos, byte[] bytes, int offset, int len) // 指定范围的内存流用于写

Writes all or part of the given byte array to the BLOB value that this Blob object represents and returns the number of bytes written.

InputStream getBinaryStream() // 获取二进制流用于读

Retrieves the BLOB value designated by this Blob instance as a stream.

InputStream getBinaryStream(long pos, long length) // 获取确定范围的二进制流用于读

Returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length.

byte[] getBytes(long pos, int length) // 获取确定范围的字节数据作为内存流读取

Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes.     
// PreparedStatement的支持:

void setAsciiStream(int parameterIndex, InputStream x) // 设置字节流以输入流读入为源设置指定列的数据

Sets the designated parameter to the given input stream.

void setAsciiStream(int parameterIndex, InputStream x, int length) // 设置指定列确定字节数的字节数据;

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

void setAsciiStream(int parameterIndex, InputStream x, long length) // 同上,存储字节数大些;

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

void setBinaryStream(int parameterIndex, InputStream x) // 设置二进制流的方式存储输入流里的二进制数据

Sets the designated parameter to the given input stream.

void setBinaryStream(int parameterIndex, InputStream x, int length)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

void setBinaryStream(int parameterIndex, InputStream x, long length)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.  
//另外: setBlob(…)  与 setClob(…)分别在指定列存储现成的Blob对象或者Clob对象!
——–存储大型文本到数据库:

package com.mldn;

import ..DriverManager;

import ..Connection;

import ..PreparedStatement;

import .io.File;

import java.io.InputStream;

import java.io.FileInputStream;

public class ClobDemo

{

/**

脚本:

create database BigOb;

use BigOb;

drop table clob;

create table clob(id int auto_increment primary key,

name varchar(80),

text longtext);

*/

// 连接驱动路径

public static final String DRIVER = “com.mysql.jdbc.Driver”; // “org.gjt.mm.mysql.driver”

// 定义数据源URL:数据库地址

public static final String DBURL = “jdbc:mysql://localhost:3306/BigOb?characterEncoding=utf8”;

// 定义数据库登录的用户名:

public static final String USERNAME = “root”;

// 定义数据库登录的密码:

public static final String PASSWORD = “123456”;

public static void main(String[] args) throws Exception // 抛出所有异常

{

Connection conn = null; // 数据库连接

PreparedStatement pstmt = null; // 预处理语句

InputStream in = null; // 准备输入流,读取文磁盘文件

// 文本文件路径

String path = File.separator + “home” + File.separator + “ubuntu” + File.separator

+ “work” + File.separator+ “mysqlcommand.txt”;

File file = new File(path); // 打开文件

in = new FileInputStream(file); // 实例化输入流

// 预插入语句

String = “INSERT INTO clob(name, text) VALUES(?, ?)”;

// 变量接口

String name = “mysql常用命令”;

// 加载数据库驱动

Class.forName(DRIVER);

// 获取数据库连接

conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);

// 预处理操作对象

pstmt = conn.prepareStatement(sql);

// 预处理设置

pstmt.setString(1, name);

pstmt.setAsciiStream(2, in, (int)file.length()); // 存储指定字节长度的文本数据

// 执行插入

pstmt.executeUpdate();

// 关闭操作与连接

in.close();

pstmt.close();

conn.close();

}

}

/* 查询mysql数据库BigOb,selct text from clob; 结果如下:

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Clob,Blob存储大对象的java操作
本文地址:https://www.zhiletu.com/archives-155.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微