Ciao ragazzi ho un problema che mi sta facendo ammattire. Ho sviluppato un applicativo che deve andare a scrivere e poi recuperare un oggetto da una tabella di un database. Fino a che ho sviluppato il codice sul mio pc dove avevo un server mysql andava tutto bene, mentre quando sono andato ad installare l'applicazione sulla macchina che la ospiterà che ha un db MSSQL sono nati i problemi. Ho cercato in rete ma non sono riuscito a trovare una soluzione che risolvesse contemporaneamente lettura e scrittura.
La scrittura l'ho risolta in questo modo
mentre la lettura che genera l'eccezione (java mssql java.io.StreamCorruptedException: invalid stream header) è la seguente:codice:public static long writeJavaObject(Connection conn, Object object) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject( object); byte[] uBytes = baos.toByteArray(); String className = object.getClass().getName(); String q = "INSERT INTO productions (name, object_value) VALUES ('"+className+"', '"+uBytes+"')"; PreparedStatement pstmt = conn.prepareStatement(q); // conn.prepareStatement(WRITE_OBJECT_SQL); pstmt.executeUpdate(); // set input parameters // pstmt.setString(1, className); // pstmt.setBlob(2, (Blob) object); //// pstmt.setBlob(0, null)(2, uBytes); // pstmt.executeUpdate(); // // get the generated key for the id // ResultSet rs = pstmt.getGeneratedKeys(); // int id = -1; // if (rs.next()) { // id = rs.getInt(1); // } // // rs.close(); pstmt.close(); System.out.println("writeJavaObject: done serializing: " + className); for(int i = 0; i < uBytes.length; i++) System.out.print(uBytes[i]+" "); System.out.println(); System.out.println("BYTES LENGTH "+uBytes.length); return 0; }
Sapresti indicarmi l'errore?codice:public static Object readJavaObject(Connection conn, long id) throws Exception { PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); // Object object = rs.getObject(1); // String className = object.getClass().getName(); rs.getString(1).getBytes(); byte[] buf =rs.getString(1).getBytes(); ByteArrayInputStream a = new ByteArrayInputStream(buf); System.out.println("BYTES read LENGTH "+buf.length); ObjectInputStream objectIn = null; if (buf != null) objectIn = new ObjectInputStream(a); Object object = objectIn.readObject(); rs.close(); pstmt.close(); System.out.println("readJavaObject: done de-serializing: " + object.getClass().getName()); return object; }
Grazie mille

Rispondi quotando

