Java reads the Oracle database BLOB field data file and saves it to a local file ******
Package Com.bo.test;import Java.io.fileoutputstream;import Java.io.inputstream;import java.sql.Connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import java.sql.Statement;/** * Example Description: * Java reads the Oracle database BLOB field data file and saves it to a local file * 1. Use Oracle's JDBC driver. * 2. The Blob field stores a file that is stored in the Blob field and saved to disk to form a file. * The program code snippet is as follows: * * @author Yaoyuanbo * @emile [email protected] * */public class Readdbio2file {public readdbio2file () {}public static void Main (String args[]) {readdbio2file test = new Readdbio2file (); if (Test.getdate ()) {System.out.prin T ("Operation succeeded! ");} else {System.out.print ("Operation Exception! ");}} public Boolean getDate () {Connection conn = null; Statement sql = null; ResultSet rs = null;try {try {//Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");//String sourceURL = "Jdbc:odbc:ORDB"; Class.forName ("Oracle.jdbc.driver.OracleDriver"); String sourceURL = "Jdbc:oracle:thin:@192.168.12.251:1521:oracle"; String user = "Hhus"; String password = "Hhus"; conn = Drivermanager.getconNection (sourceurl, user, password); sql = Conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_ updatable);//Note: "INI" field is a blob fields string sqlstr = "SELECT * from Report_file t where T.studyid = ' 6475880 '"; rs = Sql.execute Query (SQLSTR), while (Rs.next ()) {String name = rs.getstring ("Studyid"), or//use Jdbcodbcdriver below to error: Hit uncaught exception java.lang.unsupportedoperationexception//Java.sql.Blob Blob = Rs.getblob ("ini");// Note the wording here: using the OracleDriveroracle.sql.BLOB BLOB = (Oracle.sql.BLOB) rs.getblob ("Reportfile"); String filepath = "c:/" + Name + ". pdf"; SYSTEM.OUT.PRINTLN ("Output file path is:" + filepath); try {inputstream in = Blob.getbinarystream (); Build output stream FileOutputStream file = new FileOutputStream (filepath); int len = (int) blob.length (); byte[] buffer = new Byte[len] ; Build buffer while (len = in.read (buffer))! =-1) {file.write (buffer, 0, Len);} File.close (); In.close ();} catch (Exception e) {System.out.println ("I/O Exception."); return false;}}} finally {rs.close (); Sql.close (); Conn.close ();}} catch (SQLException e) {System.out.println ("SQLException."); return false;} catch (ClassNotFoundException e) {System.out.println ("classnotfoundexception."); return false;} return true;}}
Java reads the Oracle database BLOB field data file and saves it to a local file