For help, I wrote an example of reading BLOB fields from the Oracle database using Java.Code.
Only memo.
Example:
1. Use the JDBC driver of oracle.
2. The Blob field stores a file and stores the content stored in the Blob field to the disk to form a file.
ProgramThe code snippet is as follows:
---------------------
Package dbtest;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. statement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. Blob;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. outputstream;
Public class getdatafrmdb {
Public getdatafrmdb (){
}
Public static void main (string ARGs []) {
Getdatafrmdb test = new getdatafrmdb ();
If (test. getdate () {
system. Out. Print ("operation successful! ");
}< br> else {
system. Out. Print (" Operation exception! ");
}< BR >}
Public Boolean getdate (){
Connection conn = NULL;
Statement SQL = NULL;
Resultset rs = NULL;
Try {
Try {
// Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
// String sourceurl = "JDBC: ODBC: ordb ";
Class. forname ("oracle. JDBC. Driver. oracledriver ");
String sourceurl = "JDBC: oracle: thin: @ 127.0.0.1: 1521: ordb ";
String user = "thamsdt ";
String Password = "thamsdt ";
Conn = drivermanager. getconnection (sourceurl, user, password );
SQL = conn. createstatement (resultset. type_scroll_sensitive,
Resultset. concur_updatable );
// Note: the "ini" field is a blob field.
String sqlstr = "select username, INI from s_user where usercode = 'root '";
Rs = SQL .exe cutequery (sqlstr );
While (Rs. Next ()){
String name = Rs. getstring ("username ");
// The following error is returned when jdbcodbcdriver is used: Hit uncaught exception java. Lang. unsupportedoperationexception
// Java. SQL. Blob blob = Rs. getblob ("ini ");
// Note: oracledriver is used.
Oracle. SQL. Blob blob = (Oracle. SQL. Blob) Rs. getblob ("ini ");
String filepath = "C:/" + name + ". ini ";
System. Out. println ("output file path:" + filepath );
Try {
Inputstream in = blob. getbinarystream (); // create an output stream
Fileoutputstream file = new fileoutputstream (filepath );
Int Len = (INT) blob. Length ();
Byte [] buffer = new byte [Len]; // create a buffer
While (LEN = in. Read (buffer ))! =-1 ){
File. Write (buffer, 0, Len );
}
File. Close ();
In. Close ();
}
Catch (exception e ){
System. Out. println ("I/O exception .");
Return false;
}
}
}
Finally {
Rs. Close ();
SQL. Close ();
Conn. Close ();
}
}
Catch (sqlexception e ){
System. Out. println ("sqlexception .");
Return false;
}
Catch (classnotfoundexception e ){
System. Out. println ("classnotfoundexception .");
Return false;
}
Return true;
}
}
---------------------
By jrq
2008/04/15 · Jing