Java writes blob to the Oracle database

Source: Internet
Author: User
Tags psql

Package com. test;

Import java. NiO. bytebuffer;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;

Public class writeblob {

Private connection conn = NULL;

Private Final Static string Psql = "insert into testblob (numcontentid, blobcontent )"
+ "Values (?, Empty_blob ())";

Private Final Static string ssql = "select blobcontent from testblob where numcontentid =? For Update ";

Private Final Static string usql = "Update testblob set blobcontent =? Where numcontentid =? ";

// Public static string SQL _blobid = "select testblob_seq.nextval from dual ";

Public writeblob () throws exception {
Connection conn = NULL;
Try {
Conn = getconnection ();
Conn. setautocommit (false );

Bytebuffer BB = bytebuffer. Allocate (10836 );
BB. Position (0 );
BB. putint (1 );

// The contentid that calls the insertblob method should use the sequence. In this case, it is simply written to death.

// The following insert method should be the method you want. In the database, 0000 0001 0000 0000 0000 will be displayed ....
// But it does not seem that you actually use bytebuffer
Byte [] b1 = BB. Array ();
Insertblob (Conn, 1, B1 );

// The following figure shows 0000 0001 after the bytebuffer is inserted into the database.
// You do not know what you want.
BB. Flip ();
Byte [] b2 = new byte [BB. Remaining ()];
BB. Get (B2 );
Insertblob (Conn, 2, B2 );

} Catch (exception ex ){
Ex. printstacktrace ();
Conn. rollback ();
} Finally {
If (Conn! = NULL)
Conn. Close ();
}
}

Public void insertblob (connection Conn, int contentid, byte [] content)
Throws exception {
Preparedstatement pstmt = NULL;
Preparedstatement pstmt2 = NULL;
Int id = contentid;
Try {

Pstmt = conn. preparestatement (Psql );
Pstmt2 = conn. preparestatement (usql );

Pstmt. setint (1, ID );
Try {
Pstmt.exe cuteupdate (); // insert an empty object into the database
} Catch (sqlexception ex ){
Ex. printstacktrace ();
}
Pstmt = conn. preparestatement (ssql );
Pstmt. setint (1, ID );
Resultset rs = pstmt.exe cutequery (); // query newly inserted records

Oracle. SQL. Blob Pc = NULL;
While (Rs. Next ()){
PC = (Oracle. SQL. Blob) Rs. getblob (1 );
}
Byte [] DATA = content;
PC. putbytes (1, data );

Pstmt2.setblob (1, PC );
Pstmt2.setint (2, ID );
Pstmt2.executeupdate ();

} Finally {
Try {
Pstmt. Close ();
Pstmt2.close ();
} Catch (exception ee ){
}
}
Return;
}

Public static connection getconnection () throws exception {
Connection conn = NULL;
Statement stmttemp = NULL;
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Conn = drivermanager. getconnection (
"JDBC: oracle: thin: @ 192.168.1.26: 1521: orcl", "Hangzhou L", "123456 ");
Stmttemp = conn. createstatement ();
Return conn;
}

Public static void main (string [] ARGs ){
Try {
Writeblob WB = new writeblob ();
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}

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.