I have found a lot of Blob materials, but few of them can be debugged successfully. I will release their own code. You can check it out.
Write to Oracle Database
Package com;
Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import oracle. SQL. Blob;
Public class jdbctest {
Public static void main (string [] ARGs) throws sqlexception, ioexception {
Dbbean DB = new dbbean (); // your own connection class
Connection con = dB. getconnect ();
Con. setautocommit (false );
Preparedstatement prestmt = con. preparestatement ("insert into testblob (ID, blobs) values (?,?) ");
Prestmt. setint (1, 1 );
Prestmt. setblob (2, blob. empty_lob ());
Prestmt.exe cuteupdate ();
Prestmt. Close ();
Prestmt = con. preparestatement ("select blobs from testblob where id =? For Update ");
Prestmt. setint (1, 1 );
Resultset rest = prestmt.exe cutequery ();
Rest. Next ();
Blob blobb = (BLOB) Rest. getblob (1 );
Fileinputstream instream = new fileinputstream ("E:/test.txt ");
Outputstream outstream = blobb. getbinaryoutputstream ();
Byte [] Buf = new byte [1, 1024000];
Int Len;
While (LEN = instream. Read (BUF)> 0 ){
Outstream. Write (BUF, 0, Len );
}
Instream. Close ();
Outstream. Close ();
Prestmt = con. preparestatement ("Update testblob set blobs =? Where id =? ");
Prestmt. setblob (1, blobb );
Prestmt. setint (2, 1 );
Prestmt.exe cuteupdate ();
Prestmt. Close ();
Con. Commit ();
Con. Close ();
}
}
Read and Write files
Package com;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import oracle. SQL. Blob;
Public class readfile {
Public static void main (string [] ARGs) throws sqlexception, ioexception
{
File out = new file ("E:/outtest.txt ");
Java. Io. fileoutputstream fout = new fileoutputstream (out );
Byte B [] = NULL;
Dbbean DB = new dbbean ();
Connection con = dB. getconnect ();
Con. setautocommit (false );
Preparedstatement prestmt = con. preparestatement ("select blobs from testblob where id =? For Update ");
Prestmt. setint (1, 1 );
Resultset rest = prestmt.exe cutequery ();
If (rest. Next ())
{
Oracle. SQL. Blob blob2 = (BLOB) Rest. getblob (1 );
System. Out. println ("blob2 length:" + blob2.length ());
B = blob2.getbytes (1, (INT) blob2.length ());
}
Fout. Write (B, 0, B. Length );
}
}
In oracle9. 0. 2. debugging successful
I had nothing to do with the boring things yesterday. I went to the company this morning and looked at the books. It was a little fun. Let's take a look.