views:

365

answers:

7

Hi! How Can I Insert/Retrive A Java Object Into/From Sql Server? THX

+4  A: 

Serialize it.

Björn
+1  A: 

You can't just insert the object "as is" in the DB. You need to serialize it, you can either do it yourself, or implement the Serializable interface (cf here )

Clement Herreman
+1  A: 

Obvious Serialize and Save the String.

Geek
+1  A: 

Serialize it to, lets say, ByteArrayOutputStream and those bytes insert into table's column of type BLOB. You will have to use PreparedStatement to be sure bytes went up regularly.

If serialization is not feasible because you have in your object reference to some other object which cannot be serialized, than that is another story, you will have to cope with your own serialization implementation or see if something can be declared transient. Maybe you can google for JSON or something similar to see if that suits your needs.

ante.sabo
+1  A: 

Serialize is a good way, or you can use Hybernate.

Rose
A: 

Serialize it and put it in a suitably large BLOB.

Thorbjørn Ravn Andersen
A: 

There are many ways to store a Java object into a database. If you want literally store the object instance in a database so that you can retrieve it later exactly as it was, you can serialize it and store the serialized bytes to a column in one of your database tables.

More than likely, you will want to be able to query the database for the individual properties of your object. For this, you will need to break the object up into pieces and save it to a table in multiple columns using JDBC. There are many persistence frameworks that do Object-Relational Mapping (ORM) for you such as Hibernate and iBatis.

Clay