With this function we can save a serialized class into a MySQL's table.
/**
* Actualitza l'objecte serialitzat (scorm_structure) de
* l'usuari amb l'identificador indicat.
*
* Nota! Fa un update, per tant ha d'existir l'usuari!
*
* @param courseStudentName : El nom de la taula en que ho guardem.
* @param studentId : L'identificador de l'usuari.(int Type)
* @param object : Retornarà l'UserObjective, caldrà fer un cast.
* @return int Type: Retornarà l'identificador del resultset.
* @throws Exception : Llança una excepció si hi ha un error.
*/
private int writeJavaObject(
final String courseStudentName,
final int studentId,
String sqlUpdateScormStructure =
"UPDATE " + courseStudentName
+ " set scorm_structure = ? WHERE student_id = ?";
/** We already have
* private Connection conn = DriverManager.getConnection(url, userName, password);
*/
conn.prepareStatement(sqlUpdateScormStructure);
oout.writeObject(object); //serializing
// set input parameters
pstmt.setBytes(1, baos.toByteArray());
pstmt.setInt(2, studentId);
pstmt.executeUpdate();
// get the generated key for the id
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
rs.close();
pstmt.close();
return id;
}