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