• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

StreamCorruptedException  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to store some blob object into database, and while retrieving the object i get the exception.......
My code and exception as below..............

Please help me ASAP!

-Pardha

private void insertRecord() {
String query = "INSERT INTO TEST VALUES(?,?)";
PreparedStatement statement = null;
ByteArrayOutputStream regStore = null;
ObjectOutputStream regObjectStream = null;
ByteArrayInputStream regArrayStream = null;
NameVO vo = new NameVO();
vo.setFirstName("xxxxx");
vo.setLastName("yyyyyyyy");

try {
Connection con = makeConnection();
con.setAutoCommit(false);
regStore = new ByteArrayOutputStream();
regObjectStream = new ObjectOutputStream(regStore);
regObjectStream.writeObject(vo);

byte[] regBytes = regStore.toByteArray();
regArrayStream = new ByteArrayInputStream(regBytes);
con.setAutoCommit(false);
statement = con.prepareStatement(query);
statement.setString(1, "1");
statement.setBinaryStream(2, regArrayStream, regBytes.length);
statement.executeUpdate();
regObjectStream.flush();
regArrayStream.close();
regObjectStream.close();
regStore.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void readRecord() {
String query = "SELECT * FROM TEST WHERE ID = ?";
PreparedStatement statement = null;
ResultSet rs = null;
Connection con = null;
ByteArrayInputStream bais = null;

//BufferedInputStream bis = null;
ObjectInputStream ois = null;
NameVO vo = null;

try {
con = makeConnection();
statement = con.prepareStatement(query);
statement.setString(1, "1");
rs = statement.executeQuery();

byte[] regBytes = new byte[1024];

while (rs.next()) {
regBytes = rs.getBytes(2);
System.out.println(new String(regBytes));
bais = new ByteArrayInputStream(regBytes);
System.out.println("1" + bais.toString());
ois = new ObjectInputStream(bais);
System.out.println(ois.toString());
vo = (NameVO) ois.readObject();
}

System.out.println(vo.getFirstName() + " " + vo.getLastName());
bais.close();
ois.close();
statement.close();
rs.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
NameVO vo = new NameVO();
vo.setFirstName("Saurabh");
vo.setLastName("Mathur");

BlobTest test = new BlobTest();
test.insertRecord();
test.readRecord();
}



java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at util.BlobTest.readRecord(BlobTest.java:145)
at util.BlobTest.main(BlobTest.java:169)
 
I'm full of tinier men! And a tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!