7/28/10

Saving hashmap in mysql using jdbc

Hi all, i would like to share a simple example of storing haspmap in mysql db.

In order to store object into your db, the field type must be a clob.





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


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{
         
      Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/esi25may", "root", "root");
        PreparedStatement ps;
        String sql=null;
   HashMap student_related_tables = new HashMap();
        student_related_tables.put("student_award","1");
        student_related_tables.put("student_enr_hist","2");
        student_related_tables.put("student_financial_assistance","3");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);


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


        byte[] data = bos.toByteArray();


        sql="insert into test(id, testobj) values(?,?)";


        ps=conn.prepareStatement(sql);
        ps.setInt(1, 1);
        ps.setObject(2, data);
        ps.executeUpdate();


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


    }


    public Object getObject() throws Exception
    {
        Object rmObj=null;


        Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/esi25may", "root", "root");
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql=null;


        sql="select * from testwhere id=1";


        ps=conn.prepareStatement(sql);


        rs=ps.executeQuery();


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


            ObjectInputStream ins;


            try {


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


            ins = new ObjectInputStream(bais);
            HashMap student_related_tables = new HashMap();
        student_related_tables.put("student_award","1");
        student_related_tables.put("student_enr_hist","2");
        student_related_tables.put("student_financial_assistance","3");
            HashMap mc =(HashMap)ins.readObject();
                System.out.println(mc.size());
            System.out.println("Object in value ::"+mc.get("satstu_scores_customfield_2"));
            ins.close();


            }
            catch (Exception e) {


            e.printStackTrace();
            }


        }


        return rmObj;
    }


    public static void main(String[] args) {
        System.out.println(" Start getConnection Method");
        try {
            SaveObject so=new SaveObject();
    //so.saveObject();
            so.getObject();


        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("End getConnection Method");
}
}

No comments:

Post a Comment

Popular Posts