Java read image file in Oracle BLOB field source code

Source: Internet
Author: User
Tags bmp image file size getmessage

Database: Oracle 10G XE

Data source driver: Jdbc12.jar

FileName: Writeblob

Database to create a clobtest table with two fields, name (VARCHAR2), content (BLOB).

1package Dbdriver;


2


3/** *//**


4 * 2008-09-28


5 * @author Duduli


6 * email:lxyzj2000@gmail.com


7 * *


8import java.sql.*;


9import java.io.*;


10import oracle.sql.*;


11


12public class Writeblob {


13


public static void Main (string[] args) {


try {


String fName2 = "";


String fileName = "E:\\jianxin.bmp";


18//e disk downstream a jianxin.bmp image file


file F = new file (fileName);


String fName = F.getname ();


int i = Fname.lastindexof ('. ');


if (i > 0 && i < fname.length ()-1) {


fName2 = fname.substring (0,i);


24}


System.out.println (fName2);


26//get filename, go out suffix file name.


Drivermanager.registerdriver (New Oracle.jdbc.driver.OracleDriver ());


Connection conn = drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe", "System", "Duduli");


Conn.setautocommit (FALSE);


blob blob = null;


PreparedStatement pstmt = conn.preparestatement ("INSERT INTO BLOBTest (name,content) VALUES (?, Empty_blob ())") ;


pstmt.setstring (1, fName2);


pstmt.executeupdate ();


Pstmt.close ();


pstmt = conn.preparestatement ("Select content from BLOBTest where name=?") For update ");


pstmt.setstring (1, fName2);


Panax Notoginseng ResultSet rset = Pstmt.executequery ();


(Rset.next ()) {


(BLOB) Rset.getblob (1);


40}


41


fileinputstream fin = new FileInputStream (f);


System.out.println ("File size =" + fin.available ());


pstmt = conn.preparestatement ("Update blobtest set content=? where name=? ");


OutputStream out = Blob.getbinaryoutputstream ();


byte[] data = new byte[(int) fin.available ()];


fin.read (data);


out.write (data);


Fin.close ();


Out.close ();


Wuyi Pstmt.setblob (1, BLOB);


pstmt.setstring (2, fName2);


pstmt.executeupdate ();


Pstmt.close ();


Conn.commit ();


Conn.close ();


catch (SQLException e) {


System.err.println (E.getmessage ());


E.printstacktrace ();


catch (IOException e) {


System.err.println (E.getmessage ());


62}


63}


64}

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.