<十>JDBC_处理Blob类型数据



/*
* 读取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类型数据的更多相关文章
- mysql存取blob类型数据
参考网址:http://www.cnblogs.com/jway1101/p/5815658.html 首先是建表语句,需要实现将表建立好. CREATE TABLE `blobtest` ( `pr ...
- mybatis 处理CLOB/BLOB类型数据
BLOB和CLOB都是大字段类型. BLOB是按二进制来存储的,而CLOB是可以直接存储文字的. 通常像图片.文件.音乐等信息就用BLOB字段来存储,先将文件转为二进制再存储进去.文章或者是较长的文字 ...
- JDBC基础学习(三)—处理BLOB类型数据
一.BLOB类型介绍 在MySQL中,BLOB是一个二进制的大型对象,可以存储大量数据的容器,它能容纳不同大小的数据. 在MySQL中有四种BLOB类型. 实际使 ...
- 插入与读取Blob类型数据
BlobTest package com.aff.PreparedStatement; import java.io.File; import java.io.FileInputStream; imp ...
- KingbaseES blob 类型数据导入导出
KingbaseES兼容了oracle的blob数据类型.通常是用来保存二进制形式的大数据,也可以用来保存其他类型的数据. 下面来验证一下各种数据存储在数据库中形式. 建表 create table ...
- oracle 向表中插入BLOB类型数据
提示: 待插入图片必须保存到oracle主机路径上. 步骤: 1.SYSDBA权限用户创建图片所在目录 CREATE OR REPLACE DIRECTORY TEST_DIR AS 'C:\Pict ...
- 读取和写入blob类型数据
读写oracle blob类型 http://zyw090111.iteye.com/blog/607869 http://blog.csdn.net/jeryjeryjery/article/de ...
- 基于PLSQL的数据库备份方法及如何解决导出clob和blob类型数据报错的问题
基于PL/SQL的数据库备份方法 PL/SQL Developer是Oracle 数据库中用于导入或导出数据库的主要工具,本文主要介绍了利用PL/SQL Developer导入和导出数据库的过程,并对 ...
- jsp页面file标签上传图片以及blob类型数据库存取。
我的jsp页面表单如下: <form name="form1" action="/YiQu/AddUserServlet?jurisdiction=1" ...
随机推荐
- Swift 定义函数 参数 返回值
定义多参数函数 - 用func声明函数 func name(parameters) -> return type { function body } func halfOpenRangeLen ...
- 用C3中的animation和transform写的一个模仿加载的时动画效果
用用C3中的animation和transform写的一个模仿加载的时动画效果! 不多说直接上代码; html标签部分 <div class="wrap"> <h ...
- iOS10 远程推送服务器所需证书以及应用授权文件配置
推送证书制作步骤(目的:导出服务器需要的p12证书) 第一步: 打开Mac系统的"钥匙串访问"-"证书助理"-"从证书颁发机构请求证书" 取 ...
- JavaCV配置
下载javacv-1.2-bin.zip https://github.com/bytedeco/javacv 解压 在Eclipse项目 Referenced Libraries 中 Add Ext ...
- UIScrollView设置滑动的距离
设置好scrollView.width即是控制滑动的距离, scrollView.clipsToBounds = NO;控制是否显示多出的部分(可灵活运用)
- 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数
1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...
- django学习记录
1.参考资料问题: 现在django发布了1.11版本,离线文档下载引擎地址 文档下载地址 在线文档:https://docs.djangoproject.com/en/1.10/intro/tuto ...
- 将input file的选择的文件清空的两种解决方案
<input type="file" id="fileupload" name="file" /> 上传文件时,选择了文件后想清 ...
- LeetCode 368
题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...
- DataGridView控件行标题显示序号
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridViewX1.R ...