JDBC数据库操作_09CLOB文本大对象操作_BLOB

本文介绍如何在MySQL中使用CLOB和BLOB类型存储大量文本和二进制数据,包括插入字符串、文件内容及从数据库读取这些数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CLOB

Character Large Object

  1. 作用:用于存储大量的文本数据
  2. 大字段比较特殊,不同的数据库处理方式不同,大字段的操作通常是用流的方式来处理。而非一般的字段,一次即可读出数据
  3. 在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

  1. 作用:用于存储大量的二进制数据
  2. 大字段比较特殊,不同的数据库处理方式不同,大字段的操作通常是用流的方式来处理。而非一般的字段,一次即可读出数据
  3. 在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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值