欢迎来到天天文库
浏览记录
ID:6031945
大小:84.50 KB
页数:7页
时间:2017-12-31
《jdbc操纵oracle数据库中的blob字段》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、数据库执行脚本:createtablebxxx(idintprimarykey,imageblob);查询lob字段是否写入的sql语句:SELECTDBMS_LOB.GETLENGTH(image)FROMbxxx;Blob字段的写入方法一:(JDBC2.0规范)代码:importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.
2、InputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.SQLException;publicclassBlobDemo01{publicstaticfinalStringDBDRIVER="oracle.jdbc.driver.OracleDriver";publicstaticfinalStringDBURL="jdbc:oracle:thi
3、n:@localhost:1521:ORCL";publicstaticfinalStringDBUSER="scott";publicstaticfinalStringDBPASSWORD="tiger";publicstaticvoidmain(String[]args){Connectionconn=null;PreparedStatementpstmt=null;Stringsql="insertintobxxxvalues(?,?)";Filef=newFile("d:"+File.separator+"5
4、586.jpg");InputStreamin=null;try{in=newFileInputStream(f);}catch(FileNotFoundExceptione1){e1.printStackTrace();}try{Class.forName(DBDRIVER);}catch(ClassNotFoundExceptione){e.printStackTrace();}try{conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);conn.s
5、etAutoCommit(false);pstmt=conn.prepareStatement(sql);pstmt.setInt(1,1);pstmt.setBinaryStream(2,in,(int)f.length());if(pstmt.executeUpdate()>0){System.out.println("OK");conn.commit();conn.setAutoCommit(true);}}catch(Exceptione){try{conn.rollback();}catch(SQLExce
6、ptione1){e1.printStackTrace();}e.printStackTrace();}finally{try{in.close();pstmt.close();conn.close();}catch(IOExceptione){e.printStackTrace();}catch(SQLExceptione){e.printStackTrace();}}}}方法二:(古老)1:设置不自动提交。2:插入一个空的blob。3:使用行级锁定该blob字段。代码:importjava.io.Buffered
7、InputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.PrintStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importoracle.sql.BLOB;publicclass
8、BlobDemo02{publicstaticfinalStringDBDRIVER="oracle.jdbc.driver.OracleDriver";publicstaticfinalStringDBURL="jdbc:oracle:thin:@localhost:1521:ORCL";publicstaticfinalStringDBUS
此文档下载收益归作者所有