Sto utilizzando Mac OSX 10.6 con PostgreSQL 9.0. Ho scritto una semplice applicazione Java che inserisce un'immagine in un campo bytea e poi interrogare lo stesso campo per controllarlo.Cosa è cambiato tra postgres jdbc 8.4 e 9 riguardo a bytearrays?
La tabella:
CREATE TABLE test.test_table
(
id integer NOT NULL,
image bytea,
CONSTRAINT test_table_pkey PRIMARY KEY (id)
);
Il programma è qualcosa di simile:
//insert the file
PreparedStatement ps = connection.prepareStatement("INSERT INTO test.test_table(id, image) VALUES (?, ?);");
byte[] bytesFromFile = readFile("img/test1.bmp");
ps.setInt(1, 1);
ps.setBytes(2, bytesFromFile);
ps.execute();
ps.close();
PreparedStatement stmt = connection.prepareStatement("Select id,image from test.test_table");
ResultSet rs = stmt.executeQuery();
//Get the file from the BD and save it to the FS
while (rs.next()) {
String id = rs.getString(1);
InputStream imageStream = rs.getBinaryStream(2);
String imageName = OUTPUT_DIR + "/" + id + ".bmp";
FileOutputStream f = new FileOutputStream(imageName);
byte buff[] = new byte[1024];
int l;
while ((l = imageStream.read(buff)) > 0) {
f.write(buff, 0, l);
}
f.close();
System.out.println("CREATED : " + imageName);// + " size " +
}
Ecco i fatti.
Utilizzo del driver postgresql-9.0-801.jdbc4.jar esso funziona perfettamente sia in PostgreSQL 8.4 e PostgreSQL 9
Uso del driver 8.4-701.jdbc4 funziona solo in PostgreSQL 8.4.
- Utilizzo del driver 8.4-701.jdbc4 con PostgreSQL 9 non funziona. Il file estratto è diverso. Un md5 mostra che il contenuto nel database è uguale al file originale. Pertanto, suppongo che il problema sia durante l'estrazione del file.
Posso aggiornare il driver, non è un problema. La mia preoccupazione è: cosa è cambiato all'interno del protocollo di comunicazione che non è più supportato in PostgreSQL 9?
+1 tks per la risposta! In sintesi, il driver 8.4 è in attesa di dati in modalità escape, ma PostgreSQL 9 lo sta inviando in modalità esadecimale. –