如何把图片存入Oracle数据库?

有谁能给一段代码实例.?

最好有点注释.. 谢谢。。

第1个回答  2009-09-04
class.forname("oracle.jdbc.driver.oracledriver");

connection con = drivermanager.getconnection(

"jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");

//处理事务

con.setautocommit(false);

statement st = con.createstatement();

//插入一个空对象

st.executeupdate("insert into blobimg values(103,empty_blob())");//注意这里用了oracle自带的函数

//用for update方式锁定数据行

resultset rs = st.executequery(

"select contents from blobimg where id=103 for update");

if (rs.next()) {

//得到java.sql.blob对象,然后cast为oracle.sql.blob

oracle.sql.blob blob = (oracle.sql.blob) rs.getblob(1).;

//到数据库的输出流

outputstream outstream = blob.getbinaryoutputstream();

//这里用一个文件模拟输入流

file file = new file("d:"proxy.txt");

inputstream fin = new fileinputstream(file);

//将输入流写到输出流

byte[] b = new byte[blob.getbuffersize()];

int len = 0;

while ( (len = fin.read(b)) != -1) {

outstream.write(b, 0, len);

//blob.putbytes(1,b);

}

//依次关闭(注意顺序)

fin.close();

outstream.flush();

outstream.close();

con.commit();

con.close();本回答被提问者采纳
第2个回答  2009-09-04
以byte数组存入即可。
相似回答