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. change netbeans look and feel

    change netbeans look and feel: 方法一: 下载地址:https://kenai.com/projects/nbsubstance/downloads/directory/ ...

  2. Python之美[从菜鸟到高手]--深刻理解原类(metaclass)

    本来想自己写这篇文章的,可当我读了这篇文章http://blog.jobbole.com/21351/,我打消了这个念头,因为肯定写的没有人家的好,说的通俗易懂,面面俱到.就厚着面皮修改下格式,测试下 ...

  3. [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects

    Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...

  4. Codeforces #250 (Div. 2) C.The Child and Toy

    之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小 ...

  5. Understanding page frames and pages

    Memory in Linux is organized in the form of pages (typically 4 KB in size). Contiguous linear addres ...

  6. 0c-38-ARC快速入门

    2.ARC快速使用 int main(){ Student *s = [[Student alloc] init]; return 0; } 只需要写一行代码,编译器会在合适的位置释放学生对象,程序员 ...

  7. innobackupex 备份实验

    [root@localhost ~]# xtrabackup -v xtrabackup version based Linux (x86_64) (revision id: 45cda89) [ro ...

  8. 纯css3实现的动画加载特效

    之前给大家带了很多款进度加载条,今天再给大家分享一款纯css3实现的动画加载特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="wrap& ...

  9. iOS 开发调试技巧

    对于软件开发而言,调试是必须学会的技能,重要性不言而喻.对于调试的技能,基本上是可以迁移的,也就是说你以前在其他平台上掌握的很多调试技巧,很多也是可以用在iOS开发中.不同语言.不同IDE.不同平台的 ...

  10. mongodb 操作语句与sql操作语句对比

    上行:SQL 操作语句 下行:Mongo 操作语句 CREATE TABLE USERS (a Number, b Number) db.createCollection("mycoll&q ...