如何在数据库中保存序列化对象

时间:2014-11-13 05:05:33

标签: java mysql serialization

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);
    }
}

这是班级学生包含的代码 字符串ID; 字符串名称 int标记 我想序列化学生对象并将它们保存到代码中提到的mysql数据库中 我一直有一个阶级演员异常,任何人都可以帮助我 提前谢谢

2 个答案:

答案 0 :(得分:2)

一种方法是在文本中存储序列化对象,即表的属性作为文本。 检索时,它与保存时的相同,即它的序列化属性不会丢失。

答案 1 :(得分:0)

您是否需要存储对象之间的关系或仅通过id引用对象?