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