Вот рабочий пример. Одно только условие, класс CabinetSquad должен наследовать Serializable интерфейс.
List<CabinetSquad> list = new ArrayList<CabinetSquad>(Arrays.asList(
new CabinetSquad("a", 1), new CabinetSquad("b", 2), new CabinetSquad("c", 3)
));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream ooStream = new ObjectOutputStream(bout);
try {
ooStream.writeObject(list);
} finally {
try {
ooStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
byte[] byteArray = bout.toByteArray();
// ----------------------------------
Connection con = dataSource.getConnection();
try {
Statement s = con.createStatement();
s.executeUpdate("CREATE TABLE ARRAY_LIST (id int not null primary key, list blob not null)");
s.close();
// -----------------------------
PreparedStatement p = con.prepareStatement("INSERT INTO ARRAY_LIST VALUES (?,?)");
p.setInt(1, 1);
p.setBlob(2, new ByteArrayInputStream(byteArray));
p.executeUpdate();
p.close();
// ------------------------------------------------
PreparedStatement p2 = con.prepareStatement("SELECT list FROM ARRAY_LIST WHERE id = ?");
p2.setInt(1, 1);
ResultSet rs = p2.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("list");
ObjectInputStream objectInputStream = new ObjectInputStream(blob.getBinaryStream());
try {
List<CabinetSquad> recoverList = (List<CabinetSquad>) objectInputStream.readObject();
for (CabinetSquad sc : recoverList) {
System.out.println(sc);
}
} finally {
try {
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
rs.close();
p2.close();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}