您的位置:首页 > 数据库 > Oracle

使用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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: