oracle에서blob 필드를 읽는 문제 해석
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 유형 필드에 대한 액세스를 살펴보면 다른 유형 필드에 비해 다음과 같은 몇 가지 뚜렷한 특징이 있음을 알 수 있습니다.
첫째, 자동 제출을 취소해야 합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.