/*
  * 读取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. android 4.4/5.1上使用aar的问题

    在Android6.0上可以使用LOCAL_STATIC_JAVA_AAR_LIBRARIES.build脚本会把指定的aar解压缩到临时目录,并将class.jar和res添加到对应的变量. LOC ...

  2. in (1,2)and in('1,2')解决

    select wm_concat(org_name) from mstorg where instr((select pass_dists from licrequests where req_no= ...

  3. CentOS7 cacti 安装

    首先centos7 web环境的安装这里就不说了.安装cacti,首先得web环境配置好 其次添加两个用户,一个是cacti用于操作mysql的 cactimysql  一个是cacti操作Linux ...

  4. oh my zsh

    简单使用oh my zsh 安装oh my Zsh 安装zsh 安装curl或者wget 下载并安装oh my zsh: curl 下载方式curl -L https://raw.github.com ...

  5. Swift - UIBezierPath

    使用UIBezierPath可以创建基于矢量的路径.使用此类可以定义简单的形状,如椭圆.矩形或者有多个直线和曲线段组成的形状等.主要用到的该类的属性包括 moveToPoint: //设置起始点 ad ...

  6. Toad for Sqlserver

    # 设置制表符 从sqlserver拷贝的存储过程粘贴到Toad,代码变得不整齐了,这就需要设置下制表符的大小.

  7. Selenium通过WebDriver控制IE浏览器出错 Browser zoom level was set to 109%. It should be set to 100%

    错误信息: WebDriverException: Message: Unexpected error launching Internet Explorer. Browser zoom level ...

  8. JavaScript异步编程的主要解决方案—对不起,我和你不在同一个频率上

    众所周知(这也忒夸张了吧?),Javascript通过事件驱动机制,在单线程模型下,以异步的形式来实现非阻塞的IO操作.这种模式使得JavaScript在处理事务时非常高效,但这带来了很多问题,比如异 ...

  9. linux系统swappiness参数在内存与交换分区间优化

    http://blog.itpub.net/29371470/viewspace-1250975        swappiness的值的大小对如何使用swap分区是有着很大的联系的.swappine ...

  10. Future和Promise

    Future用于获取异步操作的结果,而Promise则比较抽象,无法直接猜测出其功能. Future Future最早来源于JDK的java.util.concurrent.Future,它用于代表异 ...