Java reads the Oracle database BLOB field data file and saves it to a local file

Source: Internet
Author: User
Tags getdate odbc

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.