2012-03-02 1 views
1

J'utilise ce code pour obtenir un Blob à partir d'une base de données MySql et il fonctionne très bien, quand je l'utilise pour une base de données SQLite il jette un StreamCorruptedExceptionObjectInputStream travaille pour MySql, mais pas pour 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; 

Quand je utiliser la base de données SQLite:

java.io.StreamCorruptedException: en-tête de flux incorrect: 61742E75 à java.io.ObjectInputStream.readStreamHeader (ObjectInputStream.java:797) à java.io.ObjectInputStream (ObjectInputStream.java. : 294)

Pourquoi existe-t-il un comportement différent?

Voici comment j'écrire dans la base de données:

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(); 
     } 
    } 
} 

Informations complémentaires version du pilote SQLite:

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

Comment écrivez-vous à la base de données? – Perception

+0

a ajouté cette information –

+0

Essayez le code suivant pour créer votre flux d'objets - 'Blob blob = rs.getBlob (" ivissblob "); ObjectInputStream objectIn = new ObjectInputStream (nouveau ByteArrayInputStream (blob.getBinaryStream())); ' – Perception

Répondre

1

une solution du problème donné par un collègue:

Insérer l'objet non avec la méthode .setObject, mais avec .setBytes à la place.

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(); 
     } 
    } 
} 

et la méthode 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; 
} 

Maintenant, il travaille avec SQLite et MySql

Questions connexes