java blob
package net.qysoft;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
/**
* java 中对Blob数据的操作数据库的创建代码:
* (1)db2 => create table blobTest ( id int not null generated always as identity, image blob )
*添加用户java,密码java
* (2) db2=>! net
* user java java /add java分配权限
*(3)db2 => grant select,insert,update,delete on
* table weihuachao.blobTest to user java
*
*
* @author weihuachao
* 以下代码在microsoft 2003 系统,DB2 9.0中测试成功.
*
*/
//类的定义开始------------------------------------------------------------
public class Test {
public static void main(String[] args) throws Exception {
Test test = new Test();
Connection conn = test.createConnection();
// Blob对象的插入的方法:
try {
// 创建插入语句.
java.sql.PreparedStatement preparedStatement = conn
.prepareStatement("insert into weihuachao.blobTest(image)values(?)");
//创建文件对象:
File file=new File("c:/a.jpg");
// 创建流对象:
java.io.BufferedInputStream imageInput = new java.io.BufferedInputStream(
new java.io.FileInputStream(file));
//参数赋值:
preparedStatement.setBinaryStream(1, imageInput,(int) file.length());
//执行语句
preparedStatement.executeUpdate();
//------------------------------------------------------------------
//Blob的读取工作:
java.sql.Statement st=conn.createStatement();
java.sql.ResultSet rs=st.executeQuery("select image from weihuachao.blobTest");
while(rs.next())
{
//读取Blob对象
Blob blob= (Blob) rs.getBlob(1);
//Blob对象转化为InputStream流
java.io.InputStream inputStream =blob.getBinaryStream();
//要写入的文件
File fileOutput = new File("c:/backa.jpg");
//文件的写入流的定义
FileOutputStream fo = new FileOutputStream(fileOutput);
int c;
//读取流并写入到文件中
while ((c = inputStream.read()) != -1)
fo.write(c);
//流的关闭:
fo.close();
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}catch(java.io.FileNotFoundException ex)
{
ex.printStackTrace();
}catch(java.io.IOException ex)
{
ex.printStackTrace();
}
finally
{
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/**
* 定义数据库连接的方法
*
* @return
*/
private Connection createConnection() {
Connection conn = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
conn = java.sql.DriverManager.getConnection("jdbc:db2:sample",
"java", "java");
} catch (SQLException ex1) {
ex1.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return conn;
}
}//结束.
总结: JAVA 对DB2中的BLOB对象的操作,主要是使用流的技术实现.对BLOB的实现主要步骤有:
(1)定义要写入的文件 File file=new File("c:/a.jpg");
(2)定义文件的写入流
java.io.BufferedInputStream imageInput = new java.io.BufferedInputStream(
new java.io.FileInputStream(file));
(3)使用函数写入:
preparedStatement.setBinaryStream(1, imageInput,(int) file.length());
(4)执行SQL语句:
对BLOB的读取采取的步骤有:
(1)读取Blob的对象: Blob blob= (Blob) rs.getBlob(1);
(2)把Blob的对象转化为流:
java.io.InputStream inputStream =blob.getBinaryStream();
(3)定义要写入的文件
File fileOutput = new File("c:/backa.jpg");
(4)文件的写入流的定义
FileOutputStream fo = new FileOutputStream(fileOutput);
(5)写入文件(流的写入技术,就不多讲了)
int c;
while ((c = inputStream.read()) != -1)
{
fo.write(c);
}
//END
java blob的更多相关文章
- Java BLOB 数据的插入与读取 操作
package com.lw.database; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- java blob 文件上传下载
1.文件上传 pojo中为byte[] 类型,数据库中对应为blob类型. 主要代码: FileInputStream fis = null; fis = new FileInputStream(ne ...
- Java Blob类型和String类型相互转换
1.String 转 Blob: String content = "Hello World!"; Blob blob = Hibernate.createBlob(content ...
- 7#Java基本语句语法
Java基本语句语法https://github.com/DuGuQiuBai/Java/blob/master/day03/day03%E6%80%BB%E7%BB%93.txt 1:位运算符(了解 ...
- 3#Java案例
以下内容引用Github地址https://github.com/DuGuQiuBai/Java/blob/master/day01/code/02_%E5%B8%A6%E6%B3%A8%E9%87% ...
- Java连载3-编译与运行阶段详解&JRE,JDK,JVM关系
·一. 1.JDK下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html ...
- Java连载5-标识符、关键字和字面值
一.标识符 1.标识符定义:在java源程序中凡是可以自己命名的单词 2.标识符可以标识什么元素? (1)类名(2)方法名(3)变量名(4)接口名(5)常量名 等等 3.标识符的命名要求 (1)一个合 ...
- Java连载6-变量的要求
一.数据类型的作用 (1)不同的数据有不同的类型,不同的数据类型底层会分配不同的大小的空间 (2)数据类型是指程序在运行阶段应该分配多大的内存空间 二.变量要求 变量中存储的具体的“数据”必须和变量的 ...
- Java连载14-补码简介&浮点型整数
一.补码简介 1.计算机中的符号数有三种表示方式,即为:原码.反码.补码.三种表示方法均有符号位和数值位,符号位都是0表示正数,符号位都是1表示负数. 2.计算机中的数字的存储方式:在计算机系统中,数 ...
随机推荐
- WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree
ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...
- 表格中的IE BUG
在表格应用了跨列单元格后,在IE6/7下当跨列单元格中的元素长度超过其跨列单元格中第一个单元格的宽度时会产生换行,如下所示: 解决方法: 1. 设置 table 的 'table-layout' 特性 ...
- More is better——并查集求最大集合(王道)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- 利用github和git命令,将本地项目共享到服务器上
一.步骤 1. 创建项目根目录 mkdir 文件夹名 2. 初始化文件夹 git init 3. 配置用户名和邮箱(第一次配置后,不需要再登录) git config user.name 名字 git ...
- Android SlidingMenu 使用具体解释
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/36677279 非常多APP都有側滑菜单的功能.部分APP左右都是側滑菜单~Sli ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 新的机器翻译服务
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 新的机器翻译服务 机器翻译服务也是继Wor ...
- POJ 3083:Children of the Candy Corn(DFS+BFS)
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...
- zoj 1100 - Mondriaan's Dream
题目:在m*n的地板上铺上同样的1*2的地板砖,问有多少种铺法. 分析:dp,组合,计数.经典dp问题,状态压缩. 状态:设f(i,j)为前i-1行铺满,第i行铺的状态的位表示为j时的铺砖种类数: 转 ...
- CentOS 6.4 图文安装教程(有些设置大部分教程没出现过)
http://www.jb51.net/os/78318.html CentOS 6.4 下载地址: http://www.jb51.net/softs/78243.html 1.首先,要有一张Cen ...
- LInux——安装Apache
在安装Apache的httpd的时候经常会遇到: configure: error: APR not found . Please read the documentation. configure ...