tags:

views:

34

answers:

1

I want to insert and retrieve a a user defined Object in DB,am using Mysql5.1. 1)What should me the data type for the column(is Blob is the correct answer for this question)

I am using EntityClass to Insert/Get values from the DB. 2)but to how to insert Object in database?

+1  A: 

The common way is to translate the object to a table - every field of the object is (toughly) translates to a column of the table. The term for this is object relational mapping. There is plenty of documentation of this on the web (like this) as this is one of the cornerstones of modern day enterprise development.

There are several libraries you can use, and the best is to stick to the standard called JPA - Java Persistence API. The most known libraries (all open source) are

Your user defined object will look like this :

@Entity
@Table(name = "some_table")
public class SomeObject implements Serializable {

    static final long serialVersionUID = <some value>;

    @Id
    @GeneratedValue
    protected Long id;

    @Column
    protected String name;

    @Column
    protected int value;

    // default constructor
    // getters, setters
    // equals, hashCode, toString
    // other methods
}
David Rabinowitz