Oracle blob write read/write modifications

Source: Internet
Author: User

Recently, my personal requirement is to store files in the database for convenient backup. Therefore, I wrote this class, which is not completely original. I have reference to some online materials, however, through your own tests, you can fully implement your own needs, so paste the code to share it!
You need to modify the following paths, database table names, and field names, or you can change them to configuration based on your needs! It depends on you! You can also change the jdbc tool class!
1: WriteAndReadFile class
Package com. grg. johnny. work;
 
Import java. io .*;
Import java. SQL .*;
 
Import oracle. SQL. BLOB;
 
Public class WriteAndReadFile {

Public static void main (String [] args ){
// 1: write first
String path = "D: // oracle2.zip ";
SaveFile (path );
// 2: Read again
// GetFile ("1 ");

}
 
/**
* Writing files
* To insert a file into blob, you must first Insert the null empty_blob () and then modify it.
* @ Param filePath
* @ Return
*/
Public static boolean saveFile (String filePath ){
File file = new File (filePath );
Connection conn = JdbcUtil. getConnection ();
Try {
Java. SQL. Statement st = conn. createStatement ();
Conn. setAutoCommit (false );
System. out. println ("============================== save file begin ==================== ============ ");
// St.exe cute ("insert into johnny_file values (1, 'daily life', empty_blob (), 'This is a very powerful file', to_char (sysdate, 'yyyy-MM-dd HH24: mi: ss '))");
St.exe cute ("insert into johnny_file values (2, 'daily life 2', empty_blob (), 'This is a very powerful file test', to_char (sysdate, 'yyyy-MM-dd HH24: mi: ss '))");
ResultSet rs = st
. ExecuteQuery ("select id, file_blob from johnny_file where id = 2 for update ");
If (rs. next ()){
BLOB blob = (BLOB) rs. getBlob ("file_blob ");
OutputStream outStream = blob. getBinaryOutputStream ();
InputStream fin = new FileInputStream (file );
Byte [] B = new byte [blob. getBufferSize ()];
Int len = 0;
While (len = fin. read (B ))! =-1 ){
OutStream. write (B, 0, len );
}
Fin. close ();
OutStream. flush ();
OutStream. close ();
Conn. commit ();
Conn. close ();
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return false;
} Catch (FileNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return false;
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return false;
}
System. out. println ("============================== save file end ==================== ============ ");
Return true;
}
 
/**
* Read
* Modify the read-out path based on your needs.
* @ Param id
*/
Public static void getFile (String id ){
Connection conn = JdbcUtil. getConnection ();
Java. SQL. Statement st;
Try {
St = conn. createStatement ();
System. out. println ("============================== get file begin ================== ============ ");
ResultSet rs = st
. ExecuteQuery ("select id, file_blob from johnny_file where id = '"
+ Id + "'");
If (rs. next ()){
BLOB blob = (BLOB) rs. getBlob ("file_blob ");
File file = new File ("D: // oracle.zip ");
FileOutputStream output = new FileOutputStream (file );
InputStream input = blob. getBinaryStream ();
Byte [] buffer = new byte [1024];
Int I = 0;
While (I = input. read (buffer ))! =-1 ){
Output. write (buffer, 0, I );
}
}
System. out. println ("============================== get file end ================== ============ ");
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
 
/**
* Modify blob content
*/
Public static void updateblob (String id ){
Connection conn = JdbcUtil. getConnection ();
Statement stem = null;
ResultSet rs = null;
Try {
Conn. setAutoCommit (false );
Stem = conn. createStatement ();
System. out. println ("================================ update file begin ==================== ============ ");
Rs = stem.exe cuteQuery ("select file_blob from johnny_file where id = '"
+ Id + "'for update ");
 
If (rs. next ()){
BLOB blob = (BLOB) rs. getBlob ("file_blob ");
OutputStream outStream = blob. getBinaryOutputStream ();
InputStream fin = new FileInputStream ("D: // OK .zip ");
Byte [] B = new byte [blob. getBufferSize ()];
Int len = 0;
While (len = fin. read (B ))! =-1 ){
OutStream. write (B, 0, len );
}
Fin. close ();
OutStream. flush ();
OutStream. close ();
Conn. commit ();
Conn. close ();
}
System. out. println ("================================ update file end ==================== ============ ");
} Catch (Exception ex ){
Try {
Conn. rollback ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println (ex. getMessage ());
}
}
}
 
2: The following is my own jdbc tool class.
 
Package com. grg. johnny. work;
 
Import java. SQL .*;
 
Public class JdbcUtil {
/**
* DriverName
**/
Static {
String driverName = "oracle. jdbc. driver. OracleDriver ";
Try {
Class. forName (driverName );
} Catch (Exception e ){
E. printStackTrace ();
}
}

/**
* GetConnection
**/
Public static Connection getConnection (){
String url = "jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl ";
String userName = "XXXXX ";
String pwd = "XXXXXX ";
Connection con = null;
Try {
Con = DriverManager. getConnection (url, userName, pwd );
} Catch (Exception ee ){
Ee. printStackTrace ();
}
Return con;
}

/**
* Close
**/
Public static void close (ResultSet rs, Statement stmt, Connection con ){
Try {
If (rs! = Null) {rs. close ();}
} Catch (Exception ee ){
Ee. printStackTrace ();
}
Try {
If (stmt! = Null) {stmt. close ();}
} Catch (Exception ee ){
Ee. printStackTrace ();
}
Try {
If (con! = Null) {con. close ();}
} Catch (Exception ee ){
Ee. printStackTrace ();
}
}

}
Author: "Johnny's zero degree"

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.