How to deal with clob field methods in oracle database

  • 2020-11-25 07:39:11
  • OfStack

When the knowledge base is built, it is obviously not enough to store articles in ordinary VARCHAR2. There are only 4000 bytes, not many words.
The CLOB data type can hold up to 8G data. However, this field has more particularity to be processed. Record 1.
Insert:
The SQL script has a character limit, while the SQL script contains many special characters, such as line breaks, quotation marks,
Something like that. It's a hassle. A common practice on the Internet is to insert an empty CLOB field and use the empty_clob() method to create the empty field, such as:
 
INSERT INTO T_TOPIC(TOPIC_ID,TOPIC_CONTENT) VALUES( ' 0000001',empty_clob()); 

Then use SELECT TOPIC_CONTENT FROM T_TOPIC WHERE TOPIC_ID='0000001'FOR UPDATE query statement,
To construct an updated STATEMENT and update the CLOB field after getting ResultSet.
 
ResultSet rs = pstm.executeQuery(); 
if(rs.next()){ 
oracle.sql.CLOB lob =(CLOB)rs.getClob(1); 
try { 
Writer os = lob.getCharacterOutputStream(); 
os.write(dr.getField("FLD_CONTENT").asString()); 
os.flush(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 

The insert and update operations are placed in the transaction, i.e., setAutoCommit(false) is set after getting to Connection;
Update:
Updated with SEELCT... FOR UPDATE way
You also need to set up transactions

Read:
 
CLOB clob = (CLOB)rs.getClob("FLD_CONTENT"); 
Reader reader = clob.getCharacterStream(); 
StringBuffer sb=new StringBuffer(); 
char[] cb = new char[1024]; 
try { 
for(int len = reader.read(cb);len>0;len= reader.read(cb)){ 
sb.append(cb,0,len); 
} 
} catch (IOException e) { 
throw new SQLException(" Failed to read the article content .",e); 
} 

Particularity of query:
Data tables with CLOB fields cannot be filtered using the DISTINCT keyword in SQL statements, even if the keyword does not precede the CLOB field name.
In fact, the DISTINCT keyword is valid for all fields in SQL. The CLOB field does not match like LIKE, so,
The go repeat operation cannot be performed.
Two solutions:
1. After calling the method in SQL and turning it into VARCHAR2 field, then DISTINCT, the limitation of this method is obvious.
2. Change the writing mode of SQL script to find out the collection of CLOB fields, and then filter with EXISTS keyword or IN keyword in the outer layer.

//String sqlsel2 = "select jsonbody from db_ps_listcatalog where" + 
// " listtype ='sh11' for update"; 
// String col="jsonbody"; 

public boolean updateClob(String sql,String col,String buf){ 
boolean flag=false; 
Statement stem=null; 
Connection conn=null; 
ResultSet rs=null; 
Writer wr = null; 
try{ 
conn= dp.getConnection(); 
conn.setAutoCommit(false); 
stem=conn.createStatement(); 
rs = stem.executeQuery(sql); 
if (rs.next()) { 
CLOB clob = (CLOB) rs.getClob(col); 
java.lang.reflect.Method methodToInvoke = clob.getClass().getMethod( 
"getCharacterOutputStream", (Class[]) null); 
wr = (Writer) methodToInvoke.invoke(clob, (Object[]) null); 
BufferedWriter bw = new BufferedWriter(wr); 
bw.write(buf); 
bw.flush(); 
bw.close(); 
conn.commit(); 
conn.close(); 
} 
flag=true; 
} catch (Exception ex){ 
try { 
conn.rollback(); 
} catch (SQLException e) { 
e.printStackTrace(); 
} 
} 
return flag; 
}

Related articles: