Mysql stores java objects
MySQL Set the field to blob
Save the object, serialize the object to byte[] first 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(); Get 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 methods online will have various problems, please use them with caution.
include:
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 object from stream
Thank you for reading, I hope it can help you. Thank you for your support for this site!