import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
* @author SunRain
*java连接oracle
*/
public class TestImage {

private static Connection conn = null;
private static Statement stmt = null;
private ResultSet rs = null;

static {
try {
// 加载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
// 获得连接
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@10.117.10.5:1521:dqjz1", "jwzh",
"jwzh");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 关闭所有与数据库相关的连接
*
* @param conn
* @param stmt
* @param rs
*/
public void closeAll(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 读取图片并入库
*/
public void insertDB() {
// 历史表与路径表关联查询
String sql = " select rownum, t2.oldfwbh, t2.olddybh, t2.oldbzdzbm, t1.tplj,t2.newfwbh from test_jgt t1, test_jgt_lsb_20130401 t2 where t1.fwbh = t2.oldfwbh and t1.sfdrcg='0' ";
String dir = "";
String[][] res;
try {
res = querySql(conn, sql);
if (res != null) {
// 循环取出照片
for (int i = 0; i < res.length; i++) {
dir = "C:\\test\\images\\images";
dir += "\\" + res[i][3] + "\\" + res[i][2] + "\\"
+ res[i][1] + "\\" + res[i][4];
// 获得fjbh(主键)
String seqId = getSequenceValue("SEQ_COMMON_SERIVAL_NUMBER",conn) ;
//String seqId = "1" ;
System.out.println(seqId+"----"+dir);
// 逐个插入
String[] args = {seqId,res[i][5],res[i][4],"image/pjpeg",res[i][1]} ;
inputImage(args,dir) ;
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭相应数据库连接
closeAll(rs, stmt, conn);
}
}

/**
* 向数据库中插入图片
* @param args
* @param impImageDir
*/
public void inputImage(String[] args, String impImageDir) {
try {

conn.setAutoCommit(false);// 取消自动提交功能
OutputStream os = null;
// 插入一个空对象empty_blob()
//stmt.executeUpdate("insert into image_lob (t_id, t_image) values ('"+args[0]+"', empty_blob())");
stmt.executeUpdate("insert into test_attachment (fjbh, dah, filename, data, cjsj, fjlx) values ('"+args[0]+"','"+args[1]+"','"+args[2]+"', empty_blob(), sysdate, '"+args[3]+"')");

// 锁定数据行进行更新,注意"for update"语句
//rs = stmt.executeQuery("select t_image from image_lob where t_id='" + args[0] + "' for update");
rs = stmt.executeQuery("select data from test_attachment where fjbh='" + args[0] + "' for update");

if (rs.next()) {
// 得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("data");
// 通过getBinaryOutputStream()方法获得向数据库中插入图片的"管道"
os = blob.getBinaryOutputStream();
// 读取想要存储的图片文件
InputStream is = new FileInputStream(impImageDir);
// 依次读取流字节,并输出到已定义好的数据库字段中.
int i = 0;
while ((i = is.read()) != -1) {
os.write(i);
}
}
os.flush();
os.close();
// 图片入库成功
stmt.executeUpdate("update test_jgt set sfdrcg='1' where fwbh = '"+args[4]+"'") ;
conn.commit();
conn.setAutoCommit(true);// 恢复现场
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
//e.printStackTrace();
try {
conn.rollback();
// 本地图片不存在
stmt.executeUpdate("update test_jgt set sfdrcg='2' where fwbh = '"+args[4]+"'") ;
conn.commit();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}

/**
* 从数据库里检索出图片
*/
public void outputImage() {
try {
String sql = "select image from t_image where id=1";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
oracle.sql.BLOB b = (oracle.sql.BLOB) rs.getBlob(1);
InputStream is = b.getBinaryStream();
FileOutputStream fos = new FileOutputStream(
"E:\\outputImage.jpg");
int i = 0;
while ((i = is.read()) != -1) {
fos.write(i);
}
fos.flush();
fos.close();
is.close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeAll(rs, stmt, conn);
}
}

/**
* 执行查询语句,获得返回结果
* @param conn
* @param sql
* @return
* @throws SQLException
* @throws Exception
*/
public String[][] querySql(Connection conn, String sql)
throws SQLException, Exception {
if (sql == null)
throw new Exception("无效的SQL语句!");
if (conn == null)
throw new Exception("获取数据库连接失败!");
conn.setAutoCommit(false);
ResultSet rs = null;
Statement stmt = null;
ResultSetMetaData md = null;

ArrayList aList = new ArrayList();
int rows = 0, cols;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
md = rs.getMetaData();
cols = md.getColumnCount();
while (rs.next()) {
String[] row = new String[md.getColumnCount() + 1];
for (int i = 0; i < md.getColumnCount(); i++) {
row[i] = rs.getString(i + 1);
}
aList.add(row);
}
rs.close();
rs = null;
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace(System.out);
throw new SQLException("#71:" + e.toString());
} catch (Exception e) {
e.printStackTrace(System.out);
throw e;
} finally {
if (stmt != null)
stmt.close();
stmt = null;
}

rows = aList.size();
if (rows == 0 || cols == 0) {
aList.clear();
aList = null;
return null;
}
String[][] res = new String[rows][cols];
for (int i = 0; i < rows; i++) {
Object[] row = (Object[]) aList.toArray()[i];
for (int j = 0; j < cols; j++) {
if (row[j] == null)
res[i][j] = new String("");
else
res[i][j] = new String(row[j].toString());
}
}
aList.clear();
aList = null;
return res;
}

/**
* 获得序列
* @param seqname
* @param conn
* @return
* @throws SQLException
*/
public static String getSequenceValue(String seqname, Connection conn)
throws SQLException {
ResultSet rs = null;
Statement stmt = null;
String res = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select " + seqname + ".nextval from dual");
if (rs.next()) {
res = rs.getString(1);
}
rs.close();
rs = null;
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace(System.out);
throw e;
}
return res;
}

public static void main(String[] args) {
// 从硬盘提取图片插入到数据库中
// new TestImage().inputImage();
// 从数据库中检索图片到硬盘
// new TestImage().outputImage();
new TestImage().insertDB();
}
}

 

ODBC,实现图片循环写入Oracle数据库的更多相关文章

  1. ODP方式,大批量数据写入ORACLE数据库

    项目中在同步数据的时候,需要把获得的数据DataTable,写入oracle数据库 因为System.Data.OracleClient写入方式写入大批量数据特别慢,改用Oracle.DataAcce ...

  2. C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB

    C# winform 窗体应用程序之图片上传Oracle数据库保存字段BLOB 我用的数据库是Oracle,就目前来看,许多数据库现在都倾向于Oracle数据库,对ORACLE数据库基本的操作也是必须 ...

  3. C#获取并写入ORACLE数据库中中英文字符集问题

    背景: 开发语言:C# 开发工具:VS2010 A方ORACLE数据库:中文字符集 B方ORACLE数据库:英文字符集 传递方式:webservice方式(取数据,并把取出的数据放到DataTable ...

  4. php读取xml文件内容,并循环写入mysql数据库

    <?php $dbconn = mysql_connect("localhost","root","root"); $db = mys ...

  5. Oracle数据库学习笔记

    创建表的同时插入数据:create table zhang3 as select * from zhang1;create table zhang3(id,name) as select * from ...

  6. Hibernate写入Oracle Date类型处理

    Hibernate写入Oracle数据库时,数据库设计字段为Date类型时,只能保存年月日,不能保存时分秒,如果要保存时分秒,需修改Hibernate.cfg.xml文件 <property n ...

  7. 将Oracle数据库中的数据写入Excel

    将Oracle数据库中的数据写入Excel 1.准备工作 Oracle数据库"TBYZB_FIELD_PRESSURE"表中数据如图: Excel模板(201512.xls): 2 ...

  8. [转]ODBC连接ORACLE数据库的设置

    本文转自:http://www.cppblog.com/edog/articles/1420.html 首先安装Oracle,以Oracle 817为例,作为ODBC开发者的客户端,才能使用Oracl ...

  9. 解决ODBC连接Oracle数据库报Unable to connect SQLState=08004问题

    今天用ODBC连接Oracle数据库时,报了这么一个错“Unable to connect SQLState=08004 Oracle ODBC Ora-12154”,上网查了好久都说PowerDes ...

随机推荐

  1. Deep Learning(深度学习)学习笔记整理系列之(五)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  2. iOS开发——开发必备OC篇&UITableView设置界面完整封装(四)

    设置界面完整封装(四) 简单MVC实现UITableView设置界面完善封装及拓展使用 关于使用和拓展, 其实基本上就是同UItableView,知识讲数据改一下就可以 拓展使用 1:首先定义一个数组 ...

  3. oc-15-匿名对象

    /** 匿名对象 1.访问成员变量--->只能给成员变量设置值,只能成功1次,每次都是新的对象. 2.调用方法时类似类方法: 跟类方法区别:匿名对象创建对象了,开辟空间了. */ #import ...

  4. ext2磁盘布局

    概述           本篇博客主要关注ext2文件系统的磁盘布局,即ext2会在格式化时将磁盘划分成什么样子.   ext2磁盘布局   任何Ext2分区中的第一个块从不受Ext2文件系统的管理, ...

  5. 工作随笔记 点击除div自身之外的地方,关闭自己

    <div id="showSelectOptions" style="width:100px;height:100px;background-color:red;b ...

  6. 最少javascript代码完成一个2048游戏

    原生javascript代码写的2048游戏.建议在谷歌浏览器下跑.'WASD'控制方向.演示地址请移步:http://runjs.cn/detail/bp8baf8b 直接贴代码~ html: &l ...

  7. How to installation V145 Renault CAN Clip diagnostic software

    Eobd2.fr has launched the new 2015 V145 Renault CAN Clip diagnostic tool (SP19-A and SP19-B). Here i ...

  8. MVC框架 - AJAX支持

    Ajax是异步JavaScript和XML的一个简写形式.MVC框架包含了不显眼的Ajax内置支持,通过它可以使用辅助方法,在所有的视图添加代码来定义Ajax特性. 在MVC中此特征是基于jQuery ...

  9. 【Mood-7】tell 2 my gf-miss u not sudden but always

    #sorry not coming 2 see u the national day holiday! I'm BULL in the land,fighting 4 u and babies!  # ...

  10. 获取JDK动态代理/CGLIB代理对象代理的目标对象。

    问题描述:: 我现在遇到个棘手的问题,要通过spring托管的service类保存对象,这个类是通过反射拿到的,经过实验发现这个类只能反射取得sservice实现了接口的方法,而extends类的方法 ...