15 Şubat 2013 Cuma

Veritabanından Blob veya Clob Veri Türlerini Java ile okutmak

Veritabanınlarıda veri tipi Blob veya Clob olan

datayı aşağıdaki metodlara parametre olarak

gönderip Metin olarak elde edebiliriz.

Clob için
----------------------------------------------------------------
public String readClob(Clob data) throws Exception
{
StringBuilder sb = new StringBuilder();
try {
Reader reader = data.getCharacterStream();
BufferedReader br = new BufferedReader(reader);

String line;
while(null != (line = br.readLine())) {
sb.append(line);
}
br.close();
} catch (SQLException e) {
// handle this exception
} catch (IOException e) {
// handle this exception
}
return sb.toString();
}


Blob için
----------------------------------------------------------------


public String readBlob(Object clob1) throws Exception
{
Blob clob=(Blob) clob1;
InputStream is = clob.getBinaryStream();
StringBuffer sb = new StringBuffer();

int readedBytes;
int bufferSize = 4096;

do {
byte[] bytes = new byte[bufferSize];

readedBytes = is.read(bytes);

if (readedBytes > 0) {
String readed = new String(bytes, 0, readedBytes, "UTF-16");
sb.append(readed);
}
} while (readedBytes == bufferSize);

is.close();
return sb.toString();
}

Hiç yorum yok:

Yorum Gönder