Want to store Object in MySQL database

為{幸葍}努か 提交于 2020-01-02 02:52:27

问题


I have a variable in java which return type is Object(java.lang.Object). I want to store this variable value in MySQL database without casting in any other primitive data type. Is there any data type available in MySQL related to Object?


回答1:


You can use a BLOB to store the raw data, but otherwise no, MySQL does not have a datatype specifically for a java object.

As a side note: You probably shouldn't be storing a raw object into the database, that kind of prevents you from doing any sort of queries on it.




回答2:


You must serialize the Object anyway, so you could serialize to XML or JSON aswell. A human readable storage form is what I would prefer. Have a look at Xstream for example. A great, threadsafe tool for marshalling/unmarshalling.

I assume of course, that your Object is a Bean/POJO.




回答3:


BLOB could do it. Serialize the object into a byte array and INSERT that as a BLOB.



来源:https://stackoverflow.com/questions/2747203/want-to-store-object-in-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!