oracle for jdbc thin的驱动3.0 不支持 *Statement 对象的setBinaryStream,要实现图片、word文档等写入到数据库的blob等列,需要借助于Clob、Blob等java对象。源码如下:
package com.wsysoft.lang;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;
import java.sql.ResultSet;
import org.apache.openjpa.jdbc.sql.Result;
/**
*
* 显示对oracle数据库操作blob或clob字段 Copyright: Copyright (c)2011
*
* @author: wsysoft@gmail.com
* @version: 1.0 Create at: 2011-7-17 下午09:17:05
*
* Modification History: Date Author Version Description
* ------------------------------------------------------------------
* 2011-7-17 wsysoft@gmail.com 1.0
*/
public class JDBCBlobOrClobDemo {
/**
* {方法的功能/动作描述}
*
* @param {引入参数名} {引入参数说明}
* @return {返回参数名} {返回参数说明}
* @exception {说明在某情况下,将发生什么异常}
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
OracleDataSource ods = null;
Connection conn = null;
Statement stment = null;
FileInputStream instream = null;
ResultSet result = null;
OutputStream outstream = null;
try {
ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@localhost:1521:maximo");
ods.setUser("java");
ods.setPassword("java123456");
conn = ods.getConnection();
//
/*
* String ddl =
* "CREATE TABLE MYTABLE(NAME VARCHAR2(30),IMAGE BLOB)";
*
* stment = conn.createStatement(); stment.execute(ddl);
*/
stment = conn.createStatement();
String ddl = "insert into mytable(name,image) values('仙剑奇侠',empty_blob())";
stment.execute(ddl);
conn.setAutoCommit(false);
result = stment
.executeQuery("select name,image from mytable where name='仙剑奇侠' for update");
Blob blob = null;
if (result.next()) {
blob = ((OracleResultSet) result).getBlob("image");
}
File binaryFile = new File("./honghong.jpg");
System.out.println("honghong File length = " + binaryFile.length());
instream = new FileInputStream(binaryFile);
outstream = blob.setBinaryStream(1L);
int size = ((BLOB) blob).getBufferSize();
byte[] buffer = new byte[size];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
try {
instream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outstream.close();
conn.commit();
if (!(conn.isClosed())) {
conn.close();
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ods.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
评论