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.bloboracle.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();
|