Questo sito è ancora in costruzione.
L'attuale sito ufficiale del JUG Padova è all'indirizzo www.jugpadova.it

Serialize a bean using jdbc

Sometimes, in my work as programmer, I need to serialize an object (bean) into a table, in a BLOB field, as well as retrieves bean from a ResultSet. I have realized a simple example that, using JDBC, allows to obtain this.

Here the method to fill PreparedStatement :

public static void fillPreparedStatement(PreparedStatement pst, int index,
         Serializable obj) throws Exception {

      if (obj != null) {
         ByteArrayOutputStream regStore = new ByteArrayOutputStream();

         ObjectOutputStream regObjectStream = new ObjectOutputStream(regStore);
         regObjectStream.writeObject(obj);
         byte[] regBytes = regStore.toByteArray();
         regObjectStream.close();
         regStore.close();
         ByteArrayInputStream regArrayStream = new ByteArrayInputStream(regBytes);
         pst.setBinaryStream(index, regArrayStream, regBytes.length);
      }// end of if
      else {
         pst.setNull(index, Types.BLOB);
      }
}// end of method

Here the method to retrieve bean from ResultSet.

public static Object getFromResultSet(ResultSet rs, String columnName)
         throws Exception {
      byte[] regBytes = rs.getBytes(columnName);
      ByteArrayInputStream regArrayStream = new ByteArrayInputStream(regBytes);
      ObjectInputStream regObjectStream = new ObjectInputStream(
            regArrayStream);
      return regObjectStream.readObject();
}

I tested this code using MySQL 4.1 db. Hope this example could save your valuable time. Enrico.