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!