Java::XML Blob data 를 손쉽게 읽는 방법
Java 프로그램밍을 오래 하여도 가끔은 어떻게 하는게 좋은 방법일지 헤메거나 인터넷을 뒤져보게 되는데 특히 file system 과 관련해서 이지 않을까 한다.
특히나 가끔은 데이타베이스 blob 타입으로 여러가지 형태의 데이타를 스트리밍하여 저장하게 되는데 이미지 뿐만이 아니라 많은 량의 xml format의 데이타를 저장하여 용이하게 쓰일 수 있다.
아래의 내용들은 그렇게 저장된 블랍 데이타를 손쉽게 불러 들여서 쓰는 셈플 코드이다.
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Properties;
public class Utils {
public static Properties getPropertyFromBlob(Blob blob){
Properties p = new Properties();
try {
if(blob != null){
int blobLength =(int) blob.length();
byte[] readbb = blob.getBytes(1, blobLength);
if( readbb !=null){
p.loadFromXML(new ByteArrayInputStream(readbb));
}
//release the blob and free up memory. (since JDBC 4.0)
blob.free();
}
} catch (SQLException sqle){
} catch (IOException ioe){
}
return p;
}
}
private void processPropertyBlob(Blob propertyBlob, Product product){
Properties productAttributes;
productAttributes = Utils.getPropertyFromBlob(propertyBlob);
if(productAttributes != null && product != null){
if(productAttributes.getProperty(“COLOR”) != null)
product.setAttributeColor(productAttributes.getProperty(“COLOR”));
}
}