Java reads BLOB fields from Oracle database

Source: Internet
Author: User

Test table

CREATE TABLE test_img (  ID    INTEGER not NULL,  NAME  VARCHAR2 (+),  IMAGE BLOB);

Description:

1. Using the JDBC Demo

2. Image field stores a picture


The program code is as follows:

Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.ResultSet;import Java.sql.sqlexception;import Java.sql.statement;public class Test {public static final String DRIVER = " Oracle.jdbc.driver.OracleDriver ";p ublic static final String URL =" JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL ";p ublic Static final String USERNAME = "Test";p ublic static final String PASSWORD = "Test"; static {try {class.forname (DRIVER);} CA TCH (ClassNotFoundException e) {e.printstacktrace ();}} /** * @param args */public static void main (string[] args) {if (download ()) {System.out.println ("Picture download Succeeded");} else {SYSTEM.O Ut.println ("Picture download Failed");}} public static Boolean download () {Connection conn = getconnection (); Statement sta = null; ResultSet rs = null;try {sta = conn.createstatement ();//The Image field is the domain of the blog String sql = "Select name, image from Test_img wher E id= ' 1 ' "; rs = sta.executequery (sql); wHile (Rs.next ()) {//output file name String name = Rs.getstring ("name"); Oracle.sql.BLOB BLOB = (Oracle.sql.BLOB) rs.getblob ("image" ); String filepath = "d:/" + name + ". jpg"; SYSTEM.OUT.PRINTLN ("Output file path is:" + filepath); InputStream input = Blob.getbinarystream (); FileOutputStream out = new FileOutputStream (filepath); int len = (int) blob.length (); byte buffer[] = new Byte[len];while ( Len = input.read (buffer))! =-1) {out.write (buffer, 0, Len);} Out.close (); Input.close ();}} catch (SQLException e) {e.printstacktrace (); return false;} catch (FileNotFoundException e) {e.printstacktrace (); return false;} catch (IOException e) {e.printstacktrace (); return false;} finally {try {rs.close (); Sta.close (); Conn.close ();} catch ( SQLException e) {e.printstacktrace (); return false;}} return true;} /** * Get Connection */public static Connection getconnection () {Connection conn = null;try {conn = Drivermanager.getconnect Ion (URL, USERNAME, PASSWORD);} catch (SQLException e) {e.printstacktrace ();} Return conn;}}

Deposit Image blob see: Oracle JDBC Deposit image blob

Itmyhome


Java reads BLOB fields from Oracle database

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.