package com.itheima.clob.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import org.junit.Test; import com.itheima.utils.JdbcUtil; /**
* 大二进制数据读写(图片,电影,音乐,字节码文件) Mysq: blob longblob
* @author wangli
*
*
use day16;
create table testblob(
id int primary key,
content longblob
);
*
*/
public class BlobTest02 {
//写一个图片到数据表中 测试时显示记录,注意 select id from testblob;
@Test
public void testAddBlob(){
Connection con = null;
PreparedStatement st =null; try {
con = JdbcUtil.getConnection();
String sql="insert into testblob values(?,?)";
st = con.prepareStatement(sql); //赋值
st.setInt(1, 1);
InputStream is = new FileInputStream("src/1.jpg");//找到src下的文件1.jpg
st.setBinaryStream(2, is, is.available());
st.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtil.release(null, st, con);
}
} @Test //读取一个图片
public void testReaderblob(){
Connection con = null;
PreparedStatement st =null;
ResultSet rs = null;
try {
con = JdbcUtil.getConnection();
String sql="select * from testblob where id=?";
st = con.prepareStatement(sql); //赋值
st.setInt(1, 1); //3.读取
rs = st.executeQuery();
if(rs.next()){
//结果集中有数据
InputStream is = rs.getBinaryStream("content");
OutputStream os = new FileOutputStream("d:/1.jpg");
int len=-1;
byte buffer[] = new byte[1024];
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
os.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtil.release(null, st, con);
}
}
}

最新文章

  1. JavaWeb_day07_JSP
  2. Spring(Model)
  3. WPF学习之路(十)实例:用户注册
  4. as 和 is 区别
  5. PS去掉图片上的文字的6种基本方法,动态教程
  6. CSS之Generator
  7. C++模板:读入优化
  8. Bootstrap之表格
  9. RPC服务的发布订阅实现Thrift
  10. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
  11. Elasticsearch 学习(一):入门
  12. 浅谈 Axios 在 Vue 项目中的使用
  13. Day 4-10 logging模块
  14. 34)django-上传文件,图片预览功能实现
  15. [STF手机设备管理平台]连接其它操作系统上的安卓设备实操介绍
  16. linux:基本指令ls、cd
  17. Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
  18. Shell - 简明Shell入门09 - 重定向(Redirection)
  19. 0xWS2812 STM32 driver for WS2812(B) RGB LEDs
  20. go 切片的 插入、删除

热门文章

  1. 安装LMS记
  2. 一. kafka 入门
  3. Ruby 事务Blocks
  4. 通过 命令提示符(cmd.exe)连接 Oracle 数据库
  5. 怎么查看mysql的安装目录,环境:windows+mysql+navicat
  6. ES6之箭头函数中的this
  7. [转载][效率工具推荐]Mathpix – 将图片数学公式转换为 LaTeX
  8. Linux kernel分析前的准备
  9. 网页设计与开发:HTML、CSS、JavaScript实例教程 (郑娅峰) pdf扫描版
  10. MATLAB进行无约束优化