Spring + mybatis Access Clob

Source: Internet
Author: User
Tags connection reset

Save time will be more troublesome, need to use the Select for Update update data, if not originally this piece of data, but also need to add first, add the CLOB word Gencun to Oracle.sql.CLOB.empty_lob ((), and then use the Create The For Update method queries this data, the queried CLOB field needs to be defined in clob.putstring way, and finally the Clob field can be updated to the database with the UPDATE statement. MyBatis can not complete the above complex operation, so my solution is to take the spring management link directly, and then the JDBC way to do it. It is important to note that when using raw JDBC to manipulate data, and MyBatis to the database operations to separate, if placed in the same transaction, it is easy to lock the table.

You need to be aware when querying, jdbctype= "CLOB" javatype= "java.lang.String".

The following code is attached to someone else

The way in which Blob/clob fields are handled in Oracle is special, so pay special attention to the following two points:

1. The Blob/clob is read and written in Oracle JDBC using a streaming mechanism , so be aware that you cannot read and write Blob/clob fields in batches, or you will see
Stream type cannot is used in batching exception.

2. The Oracle blob/clob field itself has a cursor,and JDBC operates on the Blob/clob field through a cursor, and cannot get its cursor handle until the Blob/clob field is created, which appears
Connection Reset by Peer:socket write error exception.

PreparedStatement ps=conn.preparestatement ("INSERT into picture (Image,resume) VALUES (?,?)");//constructs an empty Blob/clob object by Oralce.sql.blob/clob.empty_lob ()Ps.setblob (1, Oracle.sql.BLOB.empty_lob ());p S.setclob (2, Oracle.sql.CLOB.empty_lob ());p s.excuteupdate ();p s.close ();//read out the Blob/clob handle againPs=conn.preparestatement ("Select Image,resume from Picture where id=?") For Update ");p S.setint (1, 100); ResultSet RS=ps.executequery (); Rs.next (); Oracle.sql.BLOB Imgblob= (Oracle.sql.BLOB) Rs.getblob (1); Oracle.sql.CLOB Resclob= (Oracle.sql.CLOB) Rs.getclob (2); //writing binary data to a BLOBFileInputStream instream=NewFileInputStream ("C://image.jpg"); OutputStream OutStream=Imgblob.getbinaryoutputstream ();byte[] buf=New byte[10240];intLen; while(Len=instream.read (BUF) >0) {outstream.write (buf,0, Len);} Instream.close (); Outstream.cloese ();//writes a string to ClobResclob.putstring (1, "This is a Clob");//then update the Blob/clob field to the databasePs=conn.preparestatement ("Update picture set image=?") and resume=? where id=? ");p S.setblob (1, Imgblob);p S.setclob (2, Resclob);p S.setint (3, 100);p s.executeupdate ();p s.close ();

Spring + mybatis Access Clob

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.