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();
			}
		}
	}
}
发布了60 篇原创文章 · 获赞 1 · 访问量 6674

猜你喜欢

转载自blog.csdn.net/weixin_42814000/article/details/104603818
今日推荐