2011-10-20 11 views
1

私はデータベースにカスタムクラスのオブジェクトを格納し、オブジェクトを取得できる必要があるアプリケーションで作業しています後で使用してください。java.io.StreamCorruptedException:無効なストリームヘッダー:626F6775 mysql blob、javaオブジェクトとカスタムオブジェクトを使用

カスタムクラスはQuizと呼ばれ、データベースはmySQLで、オブジェクトを格納するためにblobを使用しています。

私はオブジェクトを格納することができますが、私はオブジェクトを使用するために取得しようとすると、私は次のエラーが表示されます。はい、私はグーグルをしました。私はいくつかの同様の問題を発見し、無駄に彼らのソリューションを試してみました。以下のコードの抜粋を参照し、必要な情報があれば教えてください。

ありがとうございました!

//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) 

答えて

1

私はそれを考え出しました。私は私のオブジェクトをシリアル化するのを忘れていた...それが働いた後、ここで

は、最終的なコードです:

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; 
    } 
関連する問題