A
A
Alecxandrys2015-07-16 17:27:52
Oracle
Alecxandrys, 2015-07-16 17:27:52

Converting BLOB to ArrayList?

Good day
When unloading an ArrayList from the database, an error occurs.
Error on the third line. ArrayList has been saved correctly.
java.io.EOFException is the only thing that is caught during debugging, there are no details, during normal operation the console does not display errors.

while (resultSet.next()) {
                Blob obj = resultSet.getBlob("DECK");
                ObjectInputStream obin2 = new ObjectInputStream(obj.getBinaryStream());

                ArrayList<CabinetSquad> decka = (ArrayList<CabinetSquad>) obin2.readObject();
                String name = resultSet.getString("NAME");
                String description = resultSet.getString("DESCRIPTION");
                Deck deck = new Deck(decka, name, description);
                Decks.add(deck);
            }
        } catch (SQLException | ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }

Please help me understand what the error is and extract the value from the BLOB.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evhen, 2015-07-17
@Alecxandrys

Here is a working example. There is only one condition, the CabinetSquad class must inherit the Serializable interface.

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question