使用java语言向oracle数据库中存入、取出图片及文件
2015-09-20 13:42
441 查看
其实,向数据库中存图片及其他任何文件都是一样的方法,都是用IO流以二进制的方式存入和取出的,本质上跟操作IO流没有什么差别,只是多了一个数据库访问而已。一下程序是我写的两个小Demo,一个存入文件,一个取出文件,没有任何逻辑,仅供参考。
目录结构:
数据库表结构:
将图片插入到数据库中:
从数据库中读取图片:
目录结构:
数据库表结构:
create table userimg( id number(8) primary key, pic blob );数据库工具:
package com.lym.util; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; import org.apache.commons.dbcp.BasicDataSource; /** * 数据库工具类 * * @author 刘彦民 2015年9月19日 下午11:08:28 */ public class DBUtil { //数据库连接池 private static BasicDataSource ds; /** * 初始化数据库配置 */ static{ Properties config = new Properties(); String path = "com"+File.separator+"lym"+File.separator+"config"+File.separator+"dbconfig.properties"; try { config.load(DBUtil.class.getClassLoader().getResourceAsStream(path)); } catch (IOException e) { e.printStackTrace(); } String driver = config.getProperty("jdbc.driver"); String url = config.getProperty("jdbc.url"); String username = config.getProperty("jdbc.username"); String password = config.getProperty("jdbc.password"); int maxActive = Integer.parseInt(config.getProperty("oracle.maxActive")); int maxWait = Integer.parseInt(config.getProperty("oracle.maxWait")); ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); ds.setMaxActive(maxActive);//数据库的最大连接数 ds.setMaxWait(maxWait);//数据库的延时等待时间 } /** * 连接数据库 * @return * @throws Exception */ public static Connection getConnection() throws Exception { try{ return ds.getConnection(); }catch(Exception e){ System.out.println("数据库连接失败!"); throw e; } } /** * 关闭数据库 * @param conn * @param stat * @param rs * @throws Exception */ public static void close(Connection conn, Statement stat, ResultSet rs) throws Exception { try { if (conn != null) conn.close(); if (stat != null) stat.close(); if (rs != null) rs.close(); } catch (Exception e) { System.out.println("数据库关闭失败!"); throw e; } } }
jdbc.driver=oracle.jdbc.OracleDriver jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl jdbc.username=scott jdbc.password=tiger oracle.maxActive=5 oracle.maxWait=5000
将图片插入到数据库中:
package com.lym.test; import java.io.FileInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import com.lym.util.DBUtil; /** * 把图片或文件保存到数据库中 * * @author 刘彦民 2015年9月20日 上午11:46:56 */ public class JDBCDemo2 { /** * 保存方法 * @param filename 文件名称 */ public static void saveImgOrFile(String filename) throws Exception { FileInputStream in = new FileInputStream(filename); Connection conn = null; PreparedStatement ps = null; try{ conn = DBUtil.getConnection(); String sql = "insert into userimg(id,pic) values(seq_userimg_id.NEXTVAL,?)"; ps = conn.prepareStatement(sql); ps.setBinaryStream(1, in, in.available()); ps.execute(); System.out.println("保存成功!"); }catch(Exception e){ System.out.println("保存文件失败!"); throw e; }finally{ DBUtil.close(conn, ps, null); } } public static void main(String[] args) { String filename = "D:/Thread.png"; try { saveImgOrFile(filename); } catch (Exception e) { e.printStackTrace(); } } }
从数据库中读取图片:
package com.lym.test; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.lym.util.DBUtil; /** * 从数据库中读取文件 * * @author 刘彦民 2015年9月20日 下午12:38:10 */ public class JDBCDemo3 { /** * 取出图片或文件 */ public static void readImgOrFile() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = DBUtil.getConnection(); String sql = "select id,pic from userimg where id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, 1); rs = ps.executeQuery(); if(rs.next()){ FileOutputStream out = new FileOutputStream("T.png"); Blob blob = rs.getBlob("pic"); InputStream in = blob.getBinaryStream(); byte[] data = new byte[1024*10]; int len = -1;//每次读取的字节数 while((len = in.read(data)) != -1){ out.write(data, 0, len); } out.close(); in.close(); System.out.println("文件读取完毕!"); } }catch(Exception e){ System.out.println("读取文件失败!"+e.getMessage()); throw e; }finally{ DBUtil.close(conn, ps, rs); } } public static void main(String[] args) { try { readImgOrFile(); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- Oracle客户端PL_SQL的安装
- Oracle的登录操作
- Oracle数据库安装完成之后的启动操作
- Oracle数据库的下载和安装
- oracle常用函数及示例
- 使用xshell进行导出oracle的dmp文件,加查询条件
- oracle数据库导入导出09192255
- ORACLE常用SQL语句大全
- oracle创建表空间
- sql server 导出的datetime结果 CAST(0x00009E0E0095524F AS DateTime) 如何向mysql,oracle等数据库进行转换
- oracle中函数和存储过程的区别和联系【转载竹沥半夏】
- oracle体系结构(1)
- 浅谈Oracle事务【转载竹沥半夏】
- Oracle exp imp按用户导出导入实例
- “Oracle.DataAccess.Client.OracleConnection”的类型初始值设定项引发异常。
- oracle PL/SQL(procedure language/SQL)程序设计之函数+过程+包(转)
- oracle 11g 自己主动调整
- Oracle常见操作汇总(转)
- Oracle学习笔记20150919异常处理表与方案的备份
- Oracle压缩总结2— 估计表压缩效应