Fórum Manipulando campos BLOB e CLOB com JDBC #567666
09/04/2009
0
create table teste (cadastro number(19), imagem blob, texto clob)
Statement stmt = conn.createStatement();
stmt.execute("insert into teste (cadastro, imagem, texto) values (1, empty_blob(), empty_clob())");
ResultSet rs = stmt.executeQuery("select cadastro, imagem, texto from teste where cadastro = 1 for update");
rs.next();
// copia arquivo para campo blob
Blob blob = rs.getBlob("imagem");
byte[] bbuf = new byte[1024];
InputStream bin = new FileInputStream(byteFile);
OutputStream bout = ((BLOB) blob).getBinaryOutputStream(); // específico driver oracle
// cout = clob.setCharacterStream(0);
int bytesRead = 0;
while ((bytesRead = bin.read(bbuf)) != -1) {
bout.write(bbuf, 0, bytesRead);
}
bin.close();
bout.close();
// copia arquivo para campo clob
Clob clob = rs.getClob("texto");
char[] cbuf = new char[1024];
Reader cin = new FileReader(file);
Writer cout = ((CLOB) clob).getCharacterOutputStream(); // específico driver oracle
// cout = clob.setCharacterStream(0);
int charsRead = 0;
while ((charsRead = cin.read(cbuf)) != -1) {
cout.write(cbuf, 0, charsRead);
}
cin.close();
cout.close();
// comita transação conn.commit(); rs.close();
rs = stmt.executeQuery("select cadastro, imagem, texto from teste where cadastro = 1");
rs.next();
// cria novo arquivo binário com blob
blob = rs.getBlob("imagem");
bin = blob.getBinaryStream();
bout = new FileOutputStream(byteFileDb);
while ((bytesRead = bin.read(bbuf)) != -1) {
bout.write(bbuf, 0, bytesRead);
}
bin.close();
bout.close();
// cria novo arquivo texto com clob
clob = rs.getClob("texto");
cin = clob.getCharacterStream();
cout = new FileWriter(fileDb);
while ((charsRead = cin.read(cbuf)) != -1) {
cout.write(cbuf, 0, charsRead);
}
cin.close();
cout.close();
// fecha cursor rs.close();
/**
* Compara dois arquivos caracter a caracter
* @param f1 Arquivo um
* @param f2 Arquivo dois
* @return <code>true</code> se forem iguais, <code>false</code> se não forem
*/
private static boolean equals(File f1, File f2) throws IOException {
FileReader r1 = new FileReader(f1);
FileReader r2 = new FileReader(f2);
while (true) {
int c1 = r1.read();
int c2 = r2.read();
if (c1 != c2) {
return false;
}
if (c1 == -1 && c2 == -1) {
break;
}
}
return true;
}
// arquivos binarios devem ser iguais
if (equals(byteFile, byteFileDb)) {
System.out.println(byteFile.toString() + " == " + byteFileDb.toString());
} else {
System.err.println(byteFile.toString() + " <> " + byteFileDb.toString());
}
// arquivos texto devem ser iguais
if (equals(file, fileDb)) {
System.out.println(file.toString() + " == " + fileDb.toString());
} else {
System.out.println(file.toString() + " <> " + fileDb.toString());
}
Giovane Kuhn
Curtir tópico
+ 0Posts
09/04/2009
Giovane Kuhn
Gostei + 0
09/04/2009
Vitor Pamplona
private static boolean equals(File f1, File f2) throws IOException {
if (f1.length() != f2.length()) return false;
FileReader r1 = new FileReader(f1);
FileReader r2 = new FileReader(f2);
while (true) {
int c1 = r1.read();
int c2 = r2.read();
if (c1 != c2) {
return false;
}
if (c1 == -1 && c2 == -1) {
break;
}
}
return true;
}
Gostei + 0
09/04/2009
Bruno Navarro
Gostei + 0
09/04/2009
Giovane Kuhn
Gostei + 0
09/04/2009
Bruno Navarro
Gostei + 0
09/04/2009
Giovane Kuhn
Gostei + 0
09/04/2009
Achilles Bisneto
Gostei + 0
09/04/2009
Andre Valdestilhas
Gostei + 0
09/04/2009
Gustavo Gatto
Gostei + 0
09/04/2009
Gustavo Gatto
Gostei + 0
09/04/2009
Giovane Kuhn
// hashmap a ser gravado no banco HashMap map = new HashMap(); ObjectOutputStream oout = new ObjectOutputStream(((BLOB) blob).getBinaryOutputStream()); oout.writeObject(map); oout.close();
// le hashmap do banco ObjectInputStream oin = new ObjectInputStream(blob.getBinaryStream()); HashMap map = (HashMap) oin.readObject(); oin.close();
Gostei + 0