prolink007
prolink007

Reputation: 34604

java.io.StreamCorruptedException: invalid stream header: 626F6775 using mysql blob, java Object and custom object

I am working on an application where i need to store an object of a custom class into a database and be able to retrieve the object and use it at a later time.

The custom class is called Quiz and the database is mySQL and i am using a blob to store the object.

I am able to store the object, but when i try to retrieve the object for use, i am getting the following error. Yes, i have googled. I have found some similar problems and tried their solutions to no avail. Please view the code excerpts below and let me know if you need any more information.

Thank you!

//Storing the Quiz object

public static void setQuizObject(String classId, Object quiz, int quizEnabled) {
    try {
        if (connect.isValid(0)) {
            statement = connect.createStatement();
            statement.executeUpdate("insert into " + 
                    DBHelper.MJLADB_QUIZTABLE + "(" + 
                    DBHelper.MJLADB_QUIZTABLE_CLASSID + ", " + 
                    DBHelper.MJLADB_QUIZTABLE_QUIZOBJECT + ", " + 
                    DBHelper.MJLADB_QUIZTABLE_QUIZENABLED + ") values ('" + 
                    classId + "', '" + 
                    quiz + "', '" + 
                    quizEnabled + "')");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

//Getting the quiz object (handling the conversion from resultset to an arraylist)

    public static ArrayList<Object[]> resultSetToTableModel(ResultSet row) throws SQLException {

        ArrayList<Object[]> classList = new ArrayList<Object[]>();
        ResultSetMetaData meta = row.getMetaData();

        Object cols[] = new Object[meta.getColumnCount()];
        for (int i = 0; i < cols.length; i++) {
            cols[i] = meta.getColumnLabel(i + 1);
        }
        classList.add(cols);

        while (row.next()) {
            Object data[] = new Object[cols.length];
            int quizColumn = -1;
            try {
                quizColumn = row.findColumn(DBHelper.MJLADB_QUIZTABLE_QUIZOBJECT_A);
            }
            catch (SQLException e) {
                quizColumn = -1;
            }

            for (int i = 0; i < data.length; i++) {
//              if (row.getInt(DBHelper.MJLADB_QUIZTABLE_QUIZOBJECT_A) == (i + 1)) {
                Object x;
                Quiz quiz;
                if (quizColumn == (i + 1)) {
                    InputStream is = row.getBlob(i + 1).getBinaryStream();
                    try {
                        ObjectInputStream ois = new ObjectInputStream(is);
                        x = ois.readObject();
                        quiz = (Quiz)x;
                        data[i] = quiz;
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                else {
                    data[i] = row.getObject(i + 1); 
                }
            }
//original stuff
//          Object data[] = new Object[cols.length];
//          for (int i = 0; i < data.length; i++) {
//              data[i] = row.getObject(i + 1);
//          }
            classList.add(data);
        }

        return classList;
    }

//The problem:

java.io.StreamCorruptedException: invalid stream header: 626F6775
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at school.cs321.mjla.util.DBHelper.resultSetToTableModel(DBHelper.java:533)
    at school.cs321.mjla.util.DBHelper.getTQuizList(DBHelper.java:445)
    at school.cs321.mjla.controlpanel.ControlPanelModel.<init>(ControlPanelModel.java:46)
    at school.cs321.mjla.mediator.Mediator.startControlPanel(Mediator.java:97)
    at school.cs321.mjla.mainview.MainViewController.controlPanelbutton(MainViewController.java:55)
    at school.cs321.mjla.mainview.MainView$6.actionPerformed(MainView.java:169)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Upvotes: 1

Views: 8019

Answers (1)

prolink007
prolink007

Reputation: 34604

I figured it out. I had forgot to serialize my object... OOPS!

Here is the final code once it was working:

public static ArrayList<Object[]> resultSetToTableModel(ResultSet row) throws SQLException {

        ArrayList<Object[]> classList = new ArrayList<Object[]>();
        ResultSetMetaData meta = row.getMetaData();

        Object cols[] = new Object[meta.getColumnCount()];
        for (int i = 0; i < cols.length; i++) {
            cols[i] = meta.getColumnLabel(i + 1);
        }
        classList.add(cols);

        while (row.next()) {
            Object data[] = new Object[cols.length];
            int quizColumn = -1;
            try {
                quizColumn = row.findColumn(DBHelper.MJLADB_QUIZTABLE_QUIZOBJECT_A);
                if (quizColumn > 0) {
                    System.out.println("greater than 0");
                }
            }
            catch (SQLException e) {
                quizColumn = -1;
            }

            for (int i = 0; i < data.length; i++) {
                if (quizColumn == (i + 1)) {
                    byte[] buf = row.getBytes(i + 1);
                    ObjectInputStream objectIn = null;
                    if (buf != null) {
                        try {
                            objectIn = new ObjectInputStream(new ByteArrayInputStream(buf));
                            data[i] = objectIn.readObject();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                }
                else {
                    data[i] = row.getObject(i + 1); 
                }
            }
            classList.add(data);
        }

        return classList;
    }

Upvotes: 1

Related Questions