MySQL Storage Java object instance detailed _java

Source: Internet
Author: User

MySQL Stores Java objects

MySQL Settings Field blob

Save object, first serialize 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 (); 

Gets the object using GetBytes () to deserialize the obtained byte[] 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 ways on the Internet will have various problems, please use caution.

Including:

1. Set URL parameters autodeserialize=true
2.setObject (Java instance object) query

ObjectInputStream oips = new ObjectInputStream (Rs.getbinarystream (1));
Arraylist<string> OBB = (Java Class) Oips.readobject ()//read object from stream

Thank you for reading, I hope to help you, thank you for your support for this site!

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.