/*
  * 读取BLOB数据:
  *  使用getBlob方法读取到Blob对象
  *  调用Blob的getBinaryStream(方法得到输入流,在使用IO操作
  * */
 @Test
 public void readBlob(){
  
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  try {
   conn=JDBCTools.getConnection();
   String sql="select * from customers where id=8";
   ps=conn.prepareStatement(sql);
   rs=ps.executeQuery();
   if (rs.next()) {
    int id=rs.getInt(1);
    String name=rs.getString(2);
    String email=rs.getString(3);
    Blob picture=rs.getBlob(5);
    InputStream in=picture.getBinaryStream();
    OutputStream os=new FileOutputStream("xrk.jpg");
    byte[] buffer=new byte[1024];
    int len=0;
    while((len=in.read(buffer))!=-1){
     os.write(buffer, 0, len);
    }
    os.close();
    in.close();
    System.out.println(id+"  "+name+" "+email);
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCTools.release(rs, ps, conn);
  }
 }
 
 /*
  * 插入BLOB类型的数据必须使用PreparedStatement:因为BLOB类型的数据是无法使用字符串拼写的
  * */
 @Test
 public void testInsertBlob(){
  
  Connection conn=null;
  PreparedStatement ps=null;
  
  try {
   
  conn=JDBCTools.getConnection();
  String sql="insert into customers (name,email,birth,picture)values(?,?,?,?)";
  ps=conn.prepareStatement(sql);
  ps.setString(1, "zpy");
  ps.setString(2, "zpy@.com");
  ps.setDate(3, new Date(new java.util.Date().getTime()));
  InputStream is=new FileInputStream("kk.jpg");
  ps.setBlob(4, is);
  ps.executeUpdate();
  
  } catch (Exception e) {
   JDBCTools.release(null, ps, conn);
  }
 }

<十>JDBC_处理Blob类型数据的更多相关文章

  1. mysql存取blob类型数据

    参考网址:http://www.cnblogs.com/jway1101/p/5815658.html 首先是建表语句,需要实现将表建立好. CREATE TABLE `blobtest` ( `pr ...

  2. mybatis 处理CLOB/BLOB类型数据

    BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...

  3. JDBC基础学习(三)—处理BLOB类型数据

    一.BLOB类型介绍      在MySQL中,BLOB是一个二进制的大型对象,可以存储大量数据的容器,它能容纳不同大小的数据.      在MySQL中有四种BLOB类型.          实际使 ...

  4. 插入与读取Blob类型数据

    BlobTest package com.aff.PreparedStatement; import java.io.File; import java.io.FileInputStream; imp ...

  5. KingbaseES blob 类型数据导入导出

    KingbaseES兼容了oracle的blob数据类型.通常是用来保存二进制形式的大数据,也可以用来保存其他类型的数据. 下面来验证一下各种数据存储在数据库中形式. 建表 create table ...

  6. oracle 向表中插入BLOB类型数据

    提示: 待插入图片必须保存到oracle主机路径上. 步骤: 1.SYSDBA权限用户创建图片所在目录 CREATE OR REPLACE DIRECTORY TEST_DIR AS 'C:\Pict ...

  7. 读取和写入blob类型数据

    读写oracle  blob类型 http://zyw090111.iteye.com/blog/607869 http://blog.csdn.net/jeryjeryjery/article/de ...

  8. 基于PLSQL的数据库备份方法及如何解决导出clob和blob类型数据报错的问题

    基于PL/SQL的数据库备份方法 PL/SQL Developer是Oracle 数据库中用于导入或导出数据库的主要工具,本文主要介绍了利用PL/SQL Developer导入和导出数据库的过程,并对 ...

  9. jsp页面file标签上传图片以及blob类型数据库存取。

    我的jsp页面表单如下: <form name="form1" action="/YiQu/AddUserServlet?jurisdiction=1" ...

随机推荐

  1. Bing Test -必应每日壁纸自动换

    今天向大家推荐一个桌面美化类的工具,没错就是自动更换壁纸,而且是精美的必应每日壁纸哦!绿色小巧,开机自启动,设置后每日自动更新你的桌面~ 软件名称:Bing Test 链接: http://pan.b ...

  2. LeetCode 167 Two Sum II - Input array is sorted

    Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...

  3. VC 鼠标滚轮事件控制绘图的问题

    问题描述: 在MFC中绘制数据曲线,通过鼠标滚轮来进行放大缩小操作.在使用滚轮事件时,发现如果数据量较大,会出现卡顿. 解决方案: 鼠标滚轮事件会进行重复绘图,考虑在鼠标滚轮结束之后再重绘: 在鼠标滚 ...

  4. gdb调试

    ·代码(实验楼中的代码,改了部分数值)命名为test.c int g(int x) { return x + 7; } int f(int x) { return g(x); } int main(v ...

  5. 51nod1265(判断四个点是否共面)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1265 题意:中文题诶- 思路:假设现有a, b, c, d四 ...

  6. ubuntu系统怎么分区

    首先科普下windows和linux的文件系统和主分区: 1.电脑的主分最多只有只能由四个.如果是win7和linux双系统,则windows系统可能会占到1-2个主分区,那linux最多只能有两个主 ...

  7. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  8. CozyRSS开发记录14-RSS源管理初步完工

    CozyRSS开发记录14-RSS源管理初步完工 1.添加源的响应 DialogHost.Show有几个版本的重载,加一个DialogClosingEventHandler参数.我们让添加源对话框的添 ...

  9. eclipse调试

    http://blog.csdn.net/u012176591/article/details/23297889

  10. PHP开发笔记:二维数组根据某一项来进行排序

    比如说我们现在有一个二维数组: $arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => ...