Java reads BLOB fields from the Oracle database

Source: Internet
Author: User

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

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.