Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:47:00+00:00 2026-05-27T19:47:00+00:00

My little test-program is writing a serialized object into a mysql database, then retrieving

  • 0

My little test-program is writing a serialized object into a mysql database, then retrieving it again, casting it to an object, then running it’s toString(). I’m trying to get the whole idea about Blobs on databases. (The object is a goat, that has a name, age and sex) My problem is that when I insert it into the database, it only sends the first byte, and not the rest.

First I write the object:

    String query = "INSERT INTO goats(goat) VALUES (goat = ?)";
    PreparedStatement pstmt = con.prepareStatement(query);
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(b);
    out.writeObject(goat);
    byte[] dataAsByteArray = b.toByteArray();
    pstmt.setBinaryStream(1 , new ByteArrayInputStream(dataAsByteArray), dataAsByteArray.length);

Then I retrieve it again:

    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT goat FROM goats");
    rs.next();
    Blob blob = rs.getBlob(1);
    byte[] bytes = blob.getBytes(1, (int) blob.length()); 
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream (bis);

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:47:00+00:00Added an answer on May 27, 2026 at 7:47 pm
    out.close();
    

    Seems to be missing.


    The answer seems to be that goat=? is a boolean comparison yielding 0 or 1; a typo.

    VALUES(goat = ?) should be VALUES(?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question might be a little specific but the test program im writing uses
I'm writing a little test program where I can look at all the notifications
Here is a little test program: #include <iostream> class Test { public: static void
I have a little JUnit-Test that export an Object to the FileSystem. In the
A little test program: #include <iostream> const float TEST_FLOAT = 1/60; const float TEST_A
Suppose I have written a little program by Xcode 4, how can I test
My little test program that I got out of the Official MCTS EXAM 70-562
I started writing a little program in C as a development excercise and I
The little test program below prints out: And SS Number IS =3039 I would
I wrote a little test program but I'm experiencing a syntax error in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.