Oracle jdbc stored in image blob

Source: Internet
Author: User

Create a test table

CREATE TABLE test_img (  ID    integer PRIMARY key,  name  varchar2 (+),  image blob);
Storing pictures
Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.sql.connection;import java.sql.PreparedStatement; Import Java.sql.resultset;import Java.sql.sqlexception;import Oracle.sql.blob;public class Test {public static void Ma        In (string[] args) {//TODO auto-generated method stub preparedstatement PST = NULL;        ResultSet rs = null;        Connection conn = Connectionmanager.getconnection ();        String sql = "INSERT into test_img (id,name,image) VALUES (?,?,?)";            try {PST = conn.preparestatement (SQL);            Pst.setint (1, 1);            Pst.setstring (2, "test");  Pst.setblob (3, Blob.empty_lob ());            Insert empty Object Empty_blob () int II = Pst.executeupdate ();            OutputStream OS = null; String q_sql = "Select image from test_img where id =?" For update "; Lock data row for update PST = conn.preparestatement (q_sql);            Pst.setint (1, 1);            rs = Pst.executequery ();                if (Rs.next ()) {Oracle.sql.BLOB BLOB = (Oracle.sql.BLOB) rs.getblob ("image");                OS = Blob.getbinaryoutputstream ();                InputStream is = new FileInputStream ("d:\\image.jpg");                int i = 0;                while ((i = Is.read ())! =-1) {os.write (i);            }} os.flush ();            Os.close (); Connectionmanager.closeall (RS, PST, conn);        Close resource} catch (SQLException e) {e.printstacktrace ();        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace (); }    }}
JDBC Helper Classes

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;public class ConnectionManager {public static final String DRIVER = "Oracle.jdbc.driver.Orac    Ledriver ";    public static final String URL = "Jdbc:oracle:thin: @localhost: 1521/ORCL";    public static final String USERNAME = "Test";    public static final String PASSWORD = "Test";        /** * Register database driver via static code block */static{try {class.forname (DRIVER);        } catch (ClassNotFoundException e) {e.printstacktrace (); }}/** * gets Connection * * @return */public static Connection getconnection () {Connecti        On conn = null;        try {conn = drivermanager.getconnection (Url,username,password);        }catch (SQLException e) {e.printstacktrace ();    } return conn; }/** * Close resultset * @param RS */public static void CloseresultseT (ResultSet rs) {if (rs! = null) {try {rs.close ();            } catch (SQLException e) {e.printstacktrace ();        }}}/** * Close Statement * @param st */public static void Closestatement (Statement st) {            if (st! = null) {try {st.close ();            } catch (SQLException e) {e.printstacktrace (); }}}/** * Close Connection * @param conn */public static void CloseConnection (Connection conn)            {if (conn! = null) {try {conn.close ();            } catch (SQLException e) {e.printstacktrace (); }}}/** * Close all * @param RS * @param STA * @param conn */public static void CloseAll (ResultSet rs,statement sta,connection conn)        {closeconnection (conn);        Closestatement (STA);    Closeresultset (RS); }}
effect


Itmyhome

Source: http://blog.csdn.net/itmyhome1990/article/details/41629669



Oracle jdbc stored in image blob

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.