How to save java object in database

Java object can be saved in any database after serialization of objects. To serialization of object you need to implement, serializable interface java.io.Serializable in class.Object first need to convert into binary stream and after this, binary stream can store in database with blob data type.

Blob data type in database save data as binary content. This content can be fetched and again typecast into original class object.

We are using MySql database to store java object. In JDBC, we are using setObject function to insert object in database. setObject is function of preparedstatement.

Database table

CREATE TABLE `javaobject` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `javaObject` longblob,
  PRIMARY KEY (`id`)
)

Where from we call and set object

MyClass mc=new MyClass();
mc.setSName("This is setting object");

SaveObject so=new SaveObject();

try{
so.setJavaObject(mc);
so.saveObject();

}
catch(Exception e)
{
    e.printStackTrace();
}

MyClass.java

import java.io.Serializable;

public class MyClass implements Serializable{

    public String sName=null;

    public String getSName() {
        return sName;
    }

    public void setSName(String name) {
        sName = name;
    }

}

Saving object in database

SaveObject.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SaveObject {

    public Object javaObject=null;

    public Object getJavaObject() {
        return javaObject;
    }

    public void setJavaObject(Object javaObject) {
        this.javaObject = javaObject;
    }

    public  void saveObject() throws Exception
    {
        try{
        Connection conn=/// get connection string;
        PreparedStatement ps=null;
        String sql=null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        oos.writeObject(javaObject);
        oos.flush();
        oos.close();
        bos.close();

        byte[] data = bos.toByteArray();

        sql="insert into javaobject (javaObject) values(?)";
        ps=conn.prepareStatement(sql);
        ps.setObject(1, data);
        ps.executeUpdate();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public Object getObject() throws Exception
    {
        Object rmObj=null;
        Connection conn=/// get connection string;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql=null;

        sql="select * from javaobject where id=1";

        ps=conn.prepareStatement(sql);

        rs=ps.executeQuery();

        if(rs.next())
        {
            ByteArrayInputStream bais;

            ObjectInputStream ins;

            try {

            bais = new ByteArrayInputStream(rs.getBytes("javaObject"));

            ins = new ObjectInputStream(bais);

            MyClass mc =(MyClass)ins.readObject();

            System.out.println("Object in value ::"+mc.getSName());
            ins.close();

            }
            catch (Exception e) {

            e.printStackTrace();
            }

        }

        return rmObj;
    }
}

get Object from database

try{
    SaveObject so=new SaveObject();
    so.getObject();
}
catch(Exception e)
{
   e.printStackTrace();
}
Bookmark  

 

One Response to “How to save java object in database”

  1. raaz says:

    This website is very very good. I have gone through so much java sample example website but i found this website is so comfortable. I will prefer everybody in my network. Its name proves itself……

    thx a ton..

Leave a Reply

Security Code:

 

  Random Post