CLOB(Character Large Object)
-
用于存储大量的文本数据
-
大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的,而非一般的字段,一次即可读出数据
MySQL中相关类型
-
TINYTEXT最大长度为255(2^[8]-1)字符的TEXT列
-
TEXT(M)最大长度为65535(2^[16]-1)字符的TEXT列
-
MEDIUMTEXT最大长度为16777215(2^[24]-1)字符的TEXT列
-
LONGTEXT最大长度为4294967295或4G(2^[32]-1)字符的TEXT列
BLOB (Binary Large Object)
-
用于存储大量的二进制数据
-
大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的,而非一般的字段,一次即可读出数据
Code
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试CLOB文本大对象的使用
* 将字符串,文件内容插入到数据库中的CLOB字段,将CLOB字段读取出来
* @author Matrix42
*
*/
public class Demo09 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
ps = conn.prepareStatement("insert into t_user(username,myinfo) values (?,?)");
ps.setString(1, "Matrix42");
//将文本文件输入到数据库
//ps.setClob(2, new FileReader(new File("D:/a.txt")));
//将程序中的字符串写入到数据库
//ps.setClob(2,new BufferedReader(new InputStreamReader(new ByteArrayInputStream("asasasas".getBytes()))));
//ps.execute();
ps = conn.prepareStatement("select * from t_user where id = ?");
ps.setObject(1, 6);
rs = ps.executeQuery();
while(rs.next()){
Clob clob = rs.getClob("myinfo");
Reader reader = clob.getCharacterStream();
int tmp = 0;
while((tmp=reader.read())!=-1){
System.out.print((char)tmp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//一定要分开,防止一个出现异常其余不能关闭
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps!=null) {
ps.close();
}
} catch(SQLException e){
e.printStackTrace();
}
try {
if (conn!=null) {
conn.close();
}
} catch(SQLException e){
e.printStackTrace();
}
}
}
}
MySQL中相关类型
-
TINYTBLOB最大长度为255(2^[8]-1)字符的BLOB列
-
BLOB(M)最大长度为65535(2^[16]-1)字符的BLOB列
-
MEDIUMBLOB最大长度为16777215(2^[24]-1)字符的BLOB列
-
LONGBLOB最大长度为4294967295或4G(2^[32]-1)字符的BLOB列
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试BLOB二进制大对象的使用
* @author Matrix42
*
*/
public class Demo10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
ps = conn.prepareStatement("insert into t_user(username,myinfo) values (?,?)");
ps = conn.prepareStatement("insert into t_user(username,head) values (?,?)");
ps.setObject(1, "Lorinda");
ps.setBlob(2, new FileInputStream(new File("D:100.jpg")));
ps.execute();
ps = conn.prepareStatement("select * from t_user where id = ?");
ps.setObject(1, 8);
rs = ps.executeQuery();
FileOutputStream fos = new FileOutputStream(new File("D:/head.jpg"));
while(rs.next()){
Blob blob = rs.getBlob("head");
InputStream is = blob.getBinaryStream();
int tmp = 0;
while((tmp=is.read())!=-1){
fos.write(tmp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//各种关闭就不写了
//一定要分开,防止一个出现异常其余不能关闭
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps!=null) {
ps.close();
}
} catch(SQLException e){
e.printStackTrace();
}
try {
if (conn!=null) {
conn.close();
}
} catch(SQLException e){
e.printStackTrace();
}
}
}
}