2012-03-02 3 views
1

のために私は、MySQLデータベースからBLOBを取得するには、このコードを使用して、それが正常に動作していない、私はsqliteのデータベースのためにそれを使用するとき、それはStreamCorruptedExceptionObjectInputStreamのは、MySQLのために動作しますが、sqliteの

public static SessionData getIvissSession(BigInteger id) throws IvissDatabaseException { 
    SessionData sd = null; 
    PreparedStatement pstmt = null; 
    Connection con = null; 
    try { 
     con = getConnection(); 
     pstmt = con.prepareStatement("SELECT ivissblob FROM iviss_session_table WHERE id =?"); 
     pstmt.setLong(1, Long.parseLong(id.toString())); 
     ResultSet rs = pstmt.executeQuery(); 
     while (rs.next()) { 
      byte[] ivissblob = rs.getBytes("ivissblob"); 
      ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(ivissblob)); 
      sd = (SessionData) objectIn.readObject(); 
      objectIn.close(); 
     } 

    } catch (SQLException e) { 
     throw new IvissDatabaseException(Constants.ERROR_202); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 

     try { 
      if (pstmt != null) { 
       pstmt.close(); 
      } 
      if (con != null) { 
       con.close(); 
      } 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

    } 

    return sd; 

とき、私がスローされます

java.io.StreamCorruptedException:SQLiteデータベースを使用して、無効なストリームヘッダ:java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:797)で61742E75 をjava.io.ObjectInputStreamで(ObjectInputStream.javaを。 :294)

なぜ動作が異なるのですか?

ここで私は、データベースに書き込む方法:

public static void insertIntoTable(BigInteger id, SessionData sd, byte[] rtsd, IvissWorker ivissWorker) { 
    PreparedStatement pstmt = null; 
    Connection con = null; 
    try { 
     con = getConnection(); 
     pstmt = con 
       .prepareStatement("REPLACE INTO iviss_session_table (id, ivissblob, rtblob, lastaccess) VALUES(?,?,?,?)"); 
     pstmt.setLong(1, Long.parseLong(id.toString())); 
     pstmt.setObject(2, sd); 
     pstmt.setObject(3, rtsd); 
     pstmt.setDate(4, new Date(System.currentTimeMillis())); 
     pstmt.executeUpdate(); 
    } catch (SQLException e) { 
     ivissWorker.getIvissWorkerOutputHandler().addError(Constants.ERROR_205, "", DbConfiguration.getDbUri()); 
    } finally { 
     try { 
      if (pstmt != null) { 
       pstmt.close(); 
      } 
      if (con != null) { 
       con.close(); 
      } 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

追加情報sqliteのドライバのバージョン:同僚から与えられた問題の

 <groupId>org.xerial</groupId> 
     <artifactId>sqlite-jdbc</artifactId> 
     <version>3.7.2</version> 
+0

で動作しますか? – Perception

+0

この情報を追加しました –

+0

オブジェクトストリームを作成するには、次のコードを試してください。 'Blob blob = rs.getBlob(" ivissblob "); ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(blob.getBinaryStream())); – Perception

答えて

1

ソリューション:

オブジェクトを挿入しませんメソッド.setObjectを使用しますが、代わりに.setBytesを使用します。

public static void insertIntoTable(BigInteger id, SessionData sd, byte[] rtsd, IvissWorker ivissWorker) { 
    PreparedStatement pstmt = null; 
    Connection con = null; 
    try { 
     con = getConnection(); 
     pstmt = con 
       .prepareStatement("REPLACE INTO iviss_session_table (id, ivissblob, rtblob, lastaccess) VALUES(?,?,?,?)"); 
     pstmt.setLong(1, Long.parseLong(id.toString())); 
     pstmt.setBytes(2, IvissUtil.getBytes(sd)); 
     pstmt.setObject(3, rtsd); 
     pstmt.setDate(4, new Date(System.currentTimeMillis())); 
     pstmt.executeUpdate(); 
     // System.out.println("Stored/Replaced session with ID: " + id + 
     // " in table."); 
    } catch (SQLException e) { 
     ivissWorker.getIvissWorkerOutputHandler().addError(Constants.ERROR_205, "", DbConfiguration.getDbUri()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (pstmt != null) { 
       pstmt.close(); 
      } 
      if (con != null) { 
       con.close(); 
      } 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

とgetBytesメソッド:

public static byte[] getBytes(Object obj) throws java.io.IOException { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    ObjectOutputStream oos = new ObjectOutputStream(bos); 
    oos.writeObject(obj); 
    oos.flush(); 
    oos.close(); 
    bos.close(); 
    byte[] data = bos.toByteArray(); 
    return data; 
} 

今、それはあなたがデータベースに書き込むにはどうすればよいのSQLiteとMySQL

関連する問題