title: Java, JDBC: StreamCorruptedException while trying to retrieve an object from database
body: <p>I have a problem with retrieving properly a specific object of my own class "BallotPaper" from a database. It saves properly but at the time of creating an ObjectInputStream, an  exception is thrown. </p>
  
  <p>Of course I have read many of similar questions and answers here but still cannot deal with it.</p>
  
  <p>In the database I have a table with a column of bytea type (it is postgreSQL) where I save my object. </p>
  
  <p>Here are several lines of code:</p>
  
  <pre><code>//Saving the object 'paper' of the class 'BallotPaper'
      PreparedStatement st = connection.prepareStatement(query);
      ByteArrayOutputStream byos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(byos);
      oos.writeObject(paper);
      byte[] bytePaper = byos.toByteArray();
      st.setBytes(1, bytePaper);
      st.executeUpdate();
  
      byos.close();
      oos.close();
      st.close(); 
  
  //Trying to retrieve the object:
      String query2 = "SELECT paper FROM voters WHERE id="+_id+";";
      Statement s = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
      ResultSet r = s.executeQuery(query2);
      r.first();
      bytePaper = r.getBytes(1);
      ByteArrayInputStream b = new ByteArrayInputStream(bytePaper);
      ObjectInputStream o = new ObjectInputStream(b); // *
      BallotPaper ba = (BallotPaper)o.readObject();
  </code></pre>
  
  <p>The Exception is thrown at the line before last, marked with  * . </p>
  
  <p>It looks like that:</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: BB656430
  at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
  at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source)
  at slave.VotersDB.saveFilledPaper(VotersDB.java:162)
  at slave.ClientThread.prepareResponse(ClientThread.java:50)
  at slave.ClientThread.run(ClientThread.java:72)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Thank you for all answers.</p>