java读写oracle的blob字段示例
package com.wanmei.meishu;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.filereader;
import java.io.inputstream;
import java.io.outputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.properties;
import oracle.sql.blob;
public class blobutil {
private static blobutil bu;
private string env;
public static blobutil getinstance(string env) {
bu = new blobutil();
bu.env = env;
return bu;
}
/**
* <p>得到数据库链接</p>
* @return
* @throws exception
*/
private connection getconnection() throws exception {
string driver = "oracle.jdbc.driver.oracledriver";
class.forname(driver);
string env = this.env;
properties pro = new properties();
// 读取classes 目录下的配置文件
pro.load(new filereader(class.class.getresource("/config.properties").getfile()));
string host = pro.getproperty(env + ".host");
string database = pro.getproperty(env + ".database");
string username = pro.getproperty(env + ".username");
string password = pro.getproperty(env + ".password");
string port = pro.getproperty(env + ".port");
string url = "jdbc:oracle:thin:@" + host + ":" + port +":" + database;
return drivermanager.getconnection(url, username, password);
}
/**
* <p>传入项目,任务,附件名称,文件路径,写入数据库</p>
* @param projectid
* @param taskid
* @param filename
* @param file
* @return 返回是否成功
*/
public boolean write(string projectid, string taskid, string filename, string file) {
connection conn = null;
resultset rs = null;
preparedstatement ps = null;
blob blob = null;
boolean flag = false;
try {
conn = getconnection();
conn.setautocommit(false);
string sql = "insert into ps_zp_prj_wbs_blob blb(zp_prj_id, zz_seq_num, zz_file_name, zz_image_blob) values(?, ? ,? ,empty_blob())";
ps = conn.preparestatement(sql);
ps.setstring(1, projectid);
ps.setstring(2, taskid);
ps.setstring(3, filename);
ps.executeupdate();
sql = "select zz_image_blob from ps_zp_prj_wbs_blob where zp_prj_id = ? and zz_seq_num = ? and zz_file_name = ? for update";
ps = conn.preparestatement(sql);
ps.setstring(1, projectid);
ps.setstring(2, taskid);
ps.setstring(3, filename);
rs = ps.executequery();
if(rs.next()) {
blob = (blob) rs.getblob(1);
}
inputstream in = new fileinputstream(file);
outputstream out = blob.setbinarystream(1l);
byte[] buffer = new byte[1024];
int length = -1;
while ((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
}
in.close();
out.close();
conn.commit();
conn.setautocommit(true);
flag = true;
}
catch(exception e) {
if(conn != null) {
try {
conn.rollback();
} catch (sqlexception e1) {
e1.printstacktrace();
}
}
}
finally {
try {
rs.close();
ps.close();
conn.close();
}
catch(exception e) {
e.printstacktrace();
}
}
return flag;
}
/**
* <p>根据项目id,任务id,文件名读取数据库blob字段文件,写入指定的文件路径</p>
* @param projectid
* @param taskid
* @param filename
* @param file
* @return 返回是否成功
*/
public boolean read(string projectid, string taskid, string filename, string file) {
connection conn = null;
resultset rs = null;
preparedstatement ps = null;
blob blob = null;
boolean flag = false;
try {
conn = getconnection();
string sql = "select zz_image_blob from ps_zp_prj_wbs_blob where zp_prj_id = ? and zz_seq_num = ? and zz_file_name = ?";
ps = conn.preparestatement(sql);
ps.setstring(1, projectid);
ps.setstring(2, taskid);
ps.setstring(3, filename);
rs = ps.executequery();
if(rs.next()) {
blob = (blob) rs.getblob(1);
}
inputstream in = blob.getbinarystream();
byte[] buf = new byte[1024];
int bytesin = 0;
fileoutputstream out = new fileoutputstream(file);
while ((bytesin = in.read(buf, 0, 1024)) != -1) {
out.write(buf, 0, bytesin);
}
in.close();
out.close();
flag = true;
}
catch(exception e) {
e.printstacktrace();
}
finally {
try {
rs.close();
ps.close();
conn.close();
}
catch(exception e) {
e.printstacktrace();
}
}
return flag;
}
public static void main(string[] args) {
blobutil bu = blobutil.getinstance("msdev");
system.out.println(bu.write("cb", "001", "image1", "d:\\61e44b02jw1dw4xbp2zo6j.jpg"));
system.out.println(bu.read("cb", "001", "image1", "d:\\2.jpg"));
}
}
上一篇: 安卓GreenDao框架一些进阶用法整理
下一篇: MySQL笔记之数据备份与还原的使用详解
推荐阅读
-
java读写oracle的blob字段示例
-
oracle的JDBC使用preparedStatement处理char类型字段的问题 博客分类: java JDBCOracleHibernateSQLSQL Server
-
Java的字符读写类CharArrayReader和CharArrayWriter使用示例
-
Java的字符读写类CharArrayReader和CharArrayWriter使用示例
-
Java多线程编程中线程锁与读写锁的使用示例
-
Java用GDAL读写shapefile的方法示例
-
Java多线程编程中线程锁与读写锁的使用示例
-
Java用GDAL读写shapefile的方法示例
-
java 读写Parquet格式的数据的示例代码
-
java 读写Parquet格式的数据的示例代码