JDBC—BLOB 二进制大对象的使用

BLOB(Binary Large Object)
  – 用于存储大量的二进制数据
  – 大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的。而非一般的字段,一次即可读出数据。
 
Mysql中相关类型:
  – TINYBLOB最大长度为255字节的BLOB列。
  – BLOB[(M)]最大长度为65,535字节的BLOB列。
  – MEDIUMBLOB最大长度为16,777,215字节的BLOB列。
  – LONGBLOB最大长度为4,294,967,295或4GB字节的BLOB列。

通过使用 Blob 插入图片到数据库中
/**
 *  测试BLOB  二进制大对象的使用
 *  Blob 只能处理字节流
 *  通过使用 Blob 插入图片到数据库中
 */
public class Demo10 {

	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/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			
			ps = conn.prepareStatement("insert into t_user1 (username,headImg) values (?,?)");
			ps.setString(1, "林伟茂");
			// 第二个参数为字节流
			ps.setBlob(2,new FileInputStream("E:/gongfang/JavaDemo/icon.jpeg"));
			ps.execute();
			
			
		} 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 从数据库中取出图片
/**
 * 测试BLOB 二进制大对象的使用 
 * Blob 只能处理字节流 
 * 通过使用 Blob 从数据库中取出图片
 */
public class Demo10_1 {

	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/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			// //通过使用 Blob 插入图片到数据库中
			// ps = conn.prepareStatement("insert into t_user1 (username,headImg) values
			// (?,?)");
			// ps.setString(1, "林伟茂");
			// // 第二个参数为字节流
			// ps.setBlob(2,new FileInputStream("E:/gongfang/JavaDemo/icon.jpeg"));
			// ps.execute();

			ps = conn.prepareStatement("select * from t_user1 where id = ?");
			// 这里的 1008(id)为你在数据库存储的id号(储存时的id号),需要到数据库看一下 
			ps.setObject(1, 1008);

			rs = ps.executeQuery();
			while (rs.next()) {
				// Blob需要用流来读取,其他字段直接读取
				System.out.println(rs.getInt("id"));

				Blob b = rs.getBlob("headImg");
				// 通过流读取Blob出来(通过流来操作)
				is = b.getBinaryStream(); // 获得一个输入流
				os = new FileOutputStream("d:/logo.jpeg");  // 输出流
				int temp = 0;
				// 等于 -1代表到最后了
				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();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值