2个回答
展开全部
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();
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();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询