Use java JDBC to operate oracle blob Data

Source: Internet
Author: User

[Java]/***** @ param query indicates the SQL statement * @ param idForQuery identifies the primary key * @ param value indicates the string data to be written * @ throws Exception */public static void writeBlobGBKBinaryStream (String query, int idForQuery, String value) throws Exception {PreparedStatement p = null; ResultSet rs = null; OutputStream blobWriter = null; try {p = JForumExecutionContext. getConnection (). prepareStatement (query); p. setInt (1, idForQuery); rs = P.exe cuteQuery (); rs. next (); Blob text = rs. getBlob (1); // obtain the first blob type field here. if you know the name of this field, you can also use this name if (text instanceof BLOB) {blobWriter = (BLOB) text ). getBinaryOutputStream (); // get the byte output stream} else {blobWriter = text. setBinaryStream (0);} blobWriter. write (value. getBytes ("GBK"); // write data blobW blobWriter. close ();} catch (IOException e) {throw new DatabaseException (e);} finally {if (blobWriter! = Null) {blobWriter. close ();} DbUtils. close (rs, p) ;}}note the following points: 1. This method can insert only one record, but not insert but update, because before calling this method, the table already contains a piece of data, and its blob field is null. Fill in the null value here. 2. the query parameter is a select parameter instead of an insert or update parameter. For example, SELECT vblob FROM pub_t_bu_image where datakey =? For update, do not forget for update. After the query is executed, do not forget rs. next () to locate the record. 3. Multiple blob data can be added to a record. Blob text = rs. getBlob (1); can be understood as in the select statement, I want to obtain the first blob type field. If there are multiple blob type fields, you can set different numbers. If you know the field name clearly, you can also use it directly. 4. Get the stream getBinaryOutputStream (). This method is in the oracle. SQL package and must be imported first. In addition, you need to verify whether the type is blob. It is similar in reading. We will not repeat it again, but pay attention to the same encoding method.

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.