java读取oracle数据库中blob字段

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

测试表

create table TEST_IMG(  ID    INTEGER not nullNAME  VARCHAR2(32),  IMAGE BLOB);

说明:

1、使用JDBC演示

2、IMAGE字段存储一个图片


程序代码如下:

import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Test public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"public static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"public static final String USERNAME = "test"public static final String PASSWORD = "test"static {  try {   Class.forName(DRIVER);  } catch (ClassNotFoundException e) {   e.printStackTrace();  } } /**  * @param args  */ public static void main(String[] args) {  if (download()) {   System.out.println("图片下载成功");  } else {   System.out.println("图片下载失败");  } } public static boolean download() {  Connection conn = getConnection();  Statement sta = null;  ResultSet rs = null;  try {   sta = conn.createStatement();   // image字段为BLOG字段   String sql = "Select name, image from test_img where id='1'";   rs = sta.executeQuery(sql);   while (rs.next()) {    // 输出文件名    String name = rs.getString("name");    oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image");    String filepath = "D:/" + name + ".jpg";    System.out.println("输出文件路径为:" + filepath);    InputStream input = blob.getBinaryStream();    FileOutputStream out = new FileOutputStream(filepath);    int len = (int) blob.length();    byte buffer[] = new byte[len];    while ((len = input.read(buffer)) != -1) {     out.write(buffer, 0, len);    }    out.close();    input.close();   }  } catch (SQLException e) {   e.printStackTrace();   return false;  } catch (FileNotFoundException e) {   e.printStackTrace();   return false;  } catch (IOException e) {   e.printStackTrace();   return false;  } finally {   try {    rs.close();    sta.close();    conn.close();   } catch (SQLException e) {    e.printStackTrace();    return false;   }  }  return true; } /**  * 获得Connection  */ public static Connection getConnection() {  Connection conn = null;  try {   conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);  } catch (SQLException e) {   e.printStackTrace();  }  return conn; }}

存入图片Blob参见:Oracle JDBC存入图片Blob

作者:itmyhome


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值