user3363357
user3363357

Reputation: 99

how to save a serialized object in a database

package test;

//

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

 class ssbn extends Student{
  static final String WRITE_OBJECT_SQL = "INSERT INTO java_objects(name, object_value) VALUES (?, ?)";

  static final String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE id = ?";

  public static Connection getConnection() throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/test";
    String username = "root";
    String password = "";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static long writeJavaObject(Connection conn, Object object) throws Exception {
    String className = object.getClass().getName();
    PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL, Statement.RETURN_GENERATED_KEYS);

    // set input parameters
    pstmt.setString(1, className);
    pstmt.setObject(2, object);
    pstmt.executeUpdate();

    // get the generated key for the id
    ResultSet rs = pstmt.getGeneratedKeys();
    int id = -1;
    if (rs.next()) {
      id = rs.getInt(1);
    }

    rs.close();
    pstmt.close();
    System.out.println("writeJavaObject: done serializing: " + className);
    return id;
  }

  public static Object readJavaObject(Connection conn, long id) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    Object object = rs.getObject(1);
    String className = object.getClass().getName();

    rs.close();
    pstmt.close();
    System.out.println("readJavaObject: done de-serializing: " + className);
    return object;
  }

  public static void main(String args[])throws Exception {
    Connection conn = null;
    try {
      conn = getConnection();
      System.out.println("conn=" + conn);
      conn.setAutoCommit(false);
      List<Object> list = new ArrayList<Object>();
      list.add("This is a short string.");
      list.add(new Integer(1234));
      list.add(new Date());

//      long objectID = writeJavaObject(conn, list);
//      conn.commit();
//      System.out.println("Serialized objectID => " + objectID);
//      List listFromDatabase = (List) readJavaObject(conn, objectID);
//      System.out.println("[After De-Serialization] list=" + listFromDatabase);
      Student st = new Student("001","aaa",95);
      long objID = writeJavaObject(conn, st);
      conn.commit();
        System.out.println("SERIALIZED : " + objID);
        //class cast exception
        Student ss = (Student)readJavaObject(conn, objID);

        System.out.println("DESERIALIZED : " + ss);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      conn.close();
    }
  }

    public ssbn(String id, String name, int marks) {
        super(id, name, marks);
    }
}

This is my code the class student contains String id; String name int marks and i want to serialize student objects and save them in to the mysql database mentioned in the code i keep getting a class cast exception can anyone help me thanks in advance

Upvotes: 5

Views: 20407

Answers (1)

Sunil Goli
Sunil Goli

Reputation: 459

One way would be store serialized object in text, ie attribute of the table as text. When you retrieve, it is same as it was while saving, ie its serialization property doesn't get lost.

Upvotes: 4

Related Questions