CLOB
Character Large Object
- 作用:用于存储大量的文本数据
- 大字段比较特殊,不同的数据库处理方式不同,大字段的操作通常是用流的方式来处理。而非一般的字段,一次即可读出数据
- 在MySQL中:
tinytext最大长度是255个字符的text列(2^8-1)
text(m)最大长度是65535个字符的text列(2^16-1)
mediumtext最大长度是16,777,215个字符的text列(2^24-1)
longtext最大长度是4,294,967,295或4GB字符的text列(2^32-1)
package com.first.jdbc;
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字段值取出来的操作。
*/
public class Demo09 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Reader r = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
// ps = conn.prepareStatement("insert into t_user (username,myInfo) values (?,?) ");
// ps.setString(1, "代红233");
// ps.setClob(2, new FileReader(new File("d:/代红.txt")));
//将文本文件内容直接输入到数据库中
// ps.setClob(2, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aabbccd".getBytes()))));
//流对象的字符串__字符串-字节流-字节输入流-字符流
//将程序中的字符串输入到数据库的CLOB字段中
//把数据库中的CLOB数据取出来
ps = conn.prepareStatement("select * from t_user where id=?");
ps.setObject(1, 1000);//取id为1000的数据
rs = ps.executeQuery();
while(rs.next()){
Clob c = rs.getClob("myInfo");
r = c.getCharacterStream();//获取流
int temp = 0;
while((temp=r.read())!=-1){
System.out.print((char)temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(r!=null){
r.close();
}
} catch (Exception 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();
}
}
}
}
BLOB
Binary Large Object
- 作用:用于存储大量的二进制数据
- 大字段比较特殊,不同的数据库处理方式不同,大字段的操作通常是用流的方式来处理。而非一般的字段,一次即可读出数据
- 在MySQL中:
tinyblob最大长度是255个字节的blob列(2^8-1)
blob(m)最大长度是65535个字节的blob列(2^16-1)
mediumblob最大长度是16,777,215个字节的blob列(2^24-1)
longblob最大长度是4,294,967,295或4GB字节的blob列(2^32-1)
package com.first.jdbc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
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 二进制大对象的使用
* 存图片
*
*/
public class Demo10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
// ps = conn.prepareStatement("insert into t_user (username,headImg) values (?,?) ");
// ps.setString(1, "代红");
// ps.setBlob(2, new FileInputStream("d:/my.jpg"));//处理字节流
// //图片不能太大
// ps.execute();
ps = conn.prepareStatement("select * from t_user where id=?");
ps.setObject(1, 21011);
rs = ps.executeQuery();
while(rs.next()){
Blob b = rs.getBlob("headImg");
is = b.getBinaryStream();//获取输入流
int temp = 0;
while((temp=is.read())!=-1){
System.out.println((char));
}
}
//输出到d盘,生成a.jpg
ps = conn.prepareStatement("select * from t_user where id=?");
ps.setObject(1, 21011);
rs = ps.executeQuery();
while(rs.next()){
Blob b = rs.getBlob("headImg");
is = b.getBinaryStream();//获取输入流
os = new FileOutputStream("d:/a.jpg");
int temp = 0;
while((temp=is.read())!=-1){
os.write(temp);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if(os!=null){
os.close();
}
} catch (Exception 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();
}
}
}
}
经过进一步学习,
ps.setBlob(2, new FileInputStream("d:/my.jpg"));//处理字节流
// //图片不能太大
这个问题主要是字段过短,把字段改大即可,否则会报:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'headImg' at row 1
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4185)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at mysql.connector.java@5.1.25-bin/com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
at com.first.jdbc.Demo10.main(Demo10.java:39)