java序列化和反序列化對象到mysql 的實現

How to save a complex java object in a MySQL table

If you want to save complex java objects to MySQL you can serialize and save them as BLOB in a MySQL table.

For example you have an object “complexObject” from class “ComplexObject” and you want to save it in database.

The ComplexObject class must implements Serializable interface and you can serialize the objects like this:

ByteArrayOutputStream baos;

ObjectOutputStream out;

baos = new ByteArrayOutputStream();

try {

out = new ObjectOutputStream(baos);

out.writeObject(complexObject);

out.close();

} catch (IOException e) {

e.printStackTrace();

}

byte[] byteObject = baos.toByteArray();

to deserialize the object :

ByteArrayInputStream bais;

ObjectInputStream in;

try {

bais = new ByteArrayInputStream(byteObject);

in = new ObjectInputStream(bais);

complexObject = (ComplexObject) in.readObject();

in.close();

} catch (IOException ex) {

ex.printStackTrace();

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

}

The MySQL table looks like this:

CREATE TABLE myTable(



complexObject BLOB,…

);

If you use Hibernate and Annotation you declare the complexObject transient, and a byte[] byteObject that will be persisted:

@Entity

@Table(name = “myTable”)

SomeClass{

private byte[] byteObject;

private ComplexObject complexObject;



@Transient

public ComplexObject getComplexObject() {

return complexObject;

}

public void setComplexObject(ComplexObject complexObject) {

this.complexObject = complexObject;

ByteArrayOutputStream baos;

ObjectOutputStream out;

baos = new ByteArrayOutputStream();

try {

out = new ObjectOutputStream(baos);

out.writeObject(complexObject);

out.close();

} catch (IOException e) {

e.printStackTrace();

}

this.byteObject = baos.toByteArray();

}

@Column(columnDefinition = “blob”)

public byte[] getByteObject() {

return byteObject;

}

public void setByteObject(byte[] byteObject) {

ByteArrayInputStream bais;

ObjectInputStream in;

try {

bais = new ByteArrayInputStream(byteObject);

in = new ObjectInputStream(bais);

complexObject = (ComplexObject) in.readObject();

in.close();

} catch (IOException ex) {

ex.printStackTrace();

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

}

this.byteObject= byteObject;

}

}

Attention the table column must be BLOB, not varchar.

Because the ComplexObject is transient it will not be save in database, but the byteObject will be save.

 

-------------------------------------------------------------------------------------------------

畢業後頭五年決定你的一生                                       10類最急需IT人才:Java開發者居首       

海量Android教程、開發資料和源碼                         給將成爲“Android高手”的10個建議 

成爲Java高手的25個學習目標--非常經典               Android 4.1果凍豆新特性詳解 

Java侵權訴訟Google獲勝,Android厚積薄發          面試必備:Android筆試總結 

Android高手必須掌握的28大內容和10個建議       Android平臺研發人才缺口30萬 

Android開發環境安裝和配置步驟詳細圖解            2012國內移動App開發者大調查結果 

Windows 7下搭建android開發環境步驟圖解        Android 4.0的30個突出的新特性 

Android高手要經過的6個階段和6個境界               linux下搭建Android開發環境步驟 

從IT菜鳥變爲“IT骨幹開發者”的11個建議          程序員編程技術迅速提高的終極攻略 

2012世界各國人均GDP排名,中國超泰國              2012年全國各省平均工資排行 

2012年中國大學高校排行榜(580強排名)         中國各省市面積和人口數量排名 

中國百萬開發者大調查:程序員的薪水不錯         Java高手需要越過的10座高山

周立功談嵌入式:我的25年嵌入式生涯                Android和Java語言的異同和關係 

華爲中國區手機銷量達千萬,80%爲智能機           谷歌Android碎片化嚴重

2012年中國各省GDP和人均GDP排名                 90後就業“錢景”:IT仍是最佳選擇

2012全球城市競爭力500強,69箇中國城市上榜   不要做浮躁的軟件工程師 

2012年世界500強,79家大陸香港臺灣公司上榜名單 給IT新兵的15個建議 

美國知名科技公司入門級軟件工程師的薪水排名  回顧Java經過的風風雨雨 

71道經典Android面試題和答案--重要知識點都涉及到了 

芯片巨頭海思和展訊:給中國芯片業帶來信心    海量經典Java教程、學習資料和源碼

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章