oracle에서blob 필드를 읽는 문제 해석

9105 단어
LOB 유형은 BLOB와 CLOB 두 가지로 나뉜다. BLOB는 이진 대형 이미지(Binary Large Object)로 텍스트가 아닌 바이트 흐름 데이터(예를 들어 프로그램, 이미지, 동영상 등)를 저장하는 데 적합하다.반면에 CLOB, 즉 문자형 대형 이미지(Character Large Object)는 문자 집합과 관련되어 텍스트형의 데이터(예를 들어 ㎡v사 파일, 대형 헤드 저작 등)를 저장하기에 적합하다.다음은 JDBC를 통해 Oracle 데이터베이스 LOB 유형 필드를 조작하는 몇 가지 상황을 프로그램 실례로 설명한다.
Power Designer PD 모델은 다음과 같은 두 개의 테스트용 데이터베이스 테이블을 먼저 만듭니다.
테이블을 작성하는 SQL 문은 다음과 같습니다. CREATE TABLE TESTCLOB ( ID NUMBER(3), CLOBCOL CLOB)CREATE TABLE TEST_BLOB ( ID NUMBER(3), BLOBCOL BLOB)
1. CLOB 객체에 대한 액세스
1. 데이터베이스에 새로운 CLOB 이미지 삽입
 
  
public static void clobInsert(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* CLOB */
stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
/* CLOB */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* CLOB */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* CLOB */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


2. CLOB 이미지 수정(원래 CLOB 이미지를 바탕으로 덮어쓰는 수정)
 
  
public static void clobModify(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* CLOB */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* CLOB */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


3. CLOB 이미지 교체(원래의 CLOB 이미지를 지우고 새로운 CLOB 이미지로 바꾸기)
 
  
public static void clobReplace(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* CLOB */
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
/* CLOB */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* CLOB */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


4. CLOB 이미지 읽기
 
  
public static void clobRead(String outfile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* CLOB */
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
while (rs.next()) {
/* CLOB */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* */
BufferedReader in = new BufferedReader(clob.getCharacterStream());
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
out.close();
in.close();
}
} catch (Exception ex) {
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


2. BLOB 객체에 대한 액세스
1. 데이터베이스에 새로운 BLOB 이미지 삽입
 
  
public static void blobInsert(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* BLOB */
stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
/* BLOB */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* BLOB */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* BLOB */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}
/* */
conn.setAutoCommit(defaultCommit);
}


2. BLOB 이미지 수정(원래 BLOB 이미지를 바탕으로 덮어쓰는 수정)
 
  
public static void blobModify(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* BLOB */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* BLOB */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* BLOB */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


3. BLOB 이미지 교체(원래의 BLOB 이미지를 지우고 새로운 BLOB 이미지로 바꾸기)
 
  
public static void blobReplace(String infile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* BLOB */
stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
/* BLOB */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* BLOB */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* BLOB */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


4, BLOB 이미지 읽기
 
  
public static void blobRead(String outfile) throws Exception
{
/* */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);

try {
/* BLOB */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
while (rs.next()) {
/* BLOB */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* */
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* */
conn.commit();
} catch (Exception ex) {
/* */
conn.rollback();
throw ex;
}

/* */
conn.setAutoCommit(defaultCommit);
}


위의 절차에서 LOB 유형 필드에 대한 액세스를 살펴보면 다른 유형 필드에 비해 다음과 같은 몇 가지 뚜렷한 특징이 있음을 알 수 있습니다.
첫째, 자동 제출을 취소해야 합니다.

좋은 웹페이지 즐겨찾기