MySQL stores Java objects

Source: Internet
Author: User

MySQL sets the field to blob.

Save the object, first serialize the object to byte [] Using setobject (byte [] bytes)

Bytearrayoutputstream baos = new bytearrayoutputstream (); objectoutputstream out = NULL; try {out = new objectoutputstream (baos); out. writeobject (Java Instance Object);} catch (ioexception e) {logger. error ("msg2bytes error! ", E) ;}finally {try {out. Close () ;}catch (ioexception e) {logger. Error (" msg2bytes error! ", E) ;}return baos. tobytearray ();

Getbytes () is used to obtain the object, and the obtained byte [] is deserialized into a Java object.

Bytearrayinputstream BAIS; objectinputstream in = NULL; try {BAIS = new bytearrayinputstream (bytes); In = new objectinputstream (BAIS); Return (Java class) in. readobject ();} finally {If (in! = NULL) {try {in. Close ();} catch (ioexception e) {logger. Error ("bytes2msg error! ", E );}}}

Other methods on the Internet may have various problems. Please use them with caution.

Including:

1. Set the URL parameter autodeserialize = true

2. setobject (Java Instance Object) Query

Objectinputstream oips = new objectinputstream (Rs. getbinarystream (1); arraylist <string> OBB = (Java class) oips. readobject (); // read objects from the stream

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.