Using JDBC4.0 to manipulate data methods for BLOB types in Oracle _oracle

Source: Internet
Author: User

After the launch of JDBC4.0, its features from many are being widely concerned. Here's a way to show you how to manipulate data for BLOB types in Oracle JDBC4.0.

The jar package you need

Using Ojdbc6.jar

In/meta-inf/manifest. MF can see specification-version:4.0.

Building a table

Create sequence seq_blobmodel_id start with 1 increment by 1 NoCache;
CREATE TABLE Blobmodel
(
blobid number () primary key NOT NULL,
image blob
); 

Write file to database

/**
* Save picture file to database
* @throws SQLException
* @throws ioexception
/public int Writeblob (String path) throws SQLException, ioexception{
int result = 0;
String sql = "INSERT into Blobmodel (blobid,image) VALUES (Seq_blobmodel_id.nextval,?)";
1. Create blob
blob image = Dbhelper.getconnection (). Createblob ();
2. Put the stream into a blob
outputstream out = Image.setbinarystream (1);
3. Read the picture and write the output stream
fileinputstream fis = new FileInputStream (path);
byte []buf = new byte[1024];
int len = 0;
while ((Len=fis.read (BUF))!=-1) {
out.write (buf, 0, Len);
}
result = Dbhelper.executeupdate2 (sql, new Object[]{image});//It simply encapsulates the JDBC Operation
Fis.close ();
Out.close ();
return result;
}

Read a file from a database

/**
* Read the picture file in the database
* @throws SQLException 
* @throws ioexception 
/public
void Readblob () throws SQLException, ioexception{
String sql = "Select image from Blobmodel where blobid=?";
Dbhelper.getconnection ();//
ResultSet rs = dbhelper.executequery (sql, new Object[]{1});
while (Rs.next ()) {
Blob image = Rs.getblob (1);
InputStream is = Image.getbinarystream ();
Bufferedinputstream bis = new Bufferedinputstream (is);
String Path = "img/" +new Date (). GetTime () + ". jpg";//Specify the output directory for the IMG folder under the project
bufferedoutputstream bos = new Bufferedoutputstream (new FileOutputStream (path));
byte []buf = new byte[1024];
int len = 0;
while ((Len=bis.read (BUF))!=-1) {
bos.write (Buf,0,len);
}
Bos.close ();
Bis.close ();
}

The above is a small series to introduce the use of JDBC4.0 to manipulate the data of the BLOB type in Oracle, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.