Read blob from Oracle using Java

Source: Internet
Author: User

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 import java. Io. Bytearrayoutputstream; import java. Io. FileOutputStream; import java. Io. InputStream; import java. Io. OutputStream; import java. SQL. Blob; import java. SQL. Connection; import java. SQL. DriverManager; import java. SQL. ResultSet; import java. SQL. Statement; public class Readblob { Public static void main(String args[]) { System. Out. println("Oracle Connect START.") ); Connection conn = null; String url = "Jdbc:oracle:thin: @address:p ortno:"; String dbName = "DB Name"; String driver = "Oracle.jdbc.OracleDriver"; String userName = "UserName"; String password = "password"; ResultSet rs = null; try { Class. Forname(driver). Newinstance(); conn = drivermanager. getconnection(url+dbName,userName,password); Statement stmt = conn. Createstatement(); rs =stmt. ExecuteQuery("Select Blob_column from TableName where somecondition"); Blob lob = null; While (rs. Next()) { Lob=rs. GetBlob("Blob_column"); } inputstream in = lob. Getbinarystream(); bytearrayoutputstream out = new bytearrayoutputstream(); outputstream outputstream = new fileoutputstream("Blobimage . png "); int buffersize = 1024x768; int length = (int) lob. Length(); byte[] buffer = new byte[buffersize]; While (length = in . Read(buffer)) ! =-1)         {Out . Write(buffer,0,length);         }Out . WriteTo(outputstream); In . Close(); Conn. Close(); Process p1 =Runtime. GetRuntime(). EXEC("MSPaint blobimage.png"); }catch (Exception e) { e. Printstacktrace();             } }}

Here we'll learn how to read the BLOB data from Oracle. In this code we read image and saved the same on local system and then execute MSPaint from Windows to show the saved Imag Es.

Read blob from Oracle using Java

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.