jdbc read, write, update CLOB fields

   When inserting data through the insert statement in jdbc or PL/SQL, if there is a CLOB field and the length of the inserted data exceeds 4000, an error of ORA-01704 string literal is too long will be reported.
1. java jdbc processing
        For the CLOB field, in fact, it needs to be processed by stream. The following are some processing methods collected from the Internet.
1.CharacterStream method
Java code Collection code
/*
--The statement to create the table is as follows:
create table t_clob(
        id varchar2(32) primary key,
        clobfield CLOB
        );
 */  
  
/**
 * Code example to read CLOB field
 */  
public void readClob() {  
    //Custom database connection management class   
    Connection conn = DbManager.getInstance().getConnection();  
    try {  
        PreparedStatement stat = conn  
                .prepareStatement("select clobfield from t_clob where id='1'");  
        ResultSet rs = stat.executeQuery();  
        if (rs.next()) {  
            oracle.sql.CLOB clob = (oracle.sql.CLOB) rs  
                    .getClob("clobfield");  
            String value = clob.getSubString(1, (int) clob.length());  
            System.out.println("CLOB field value: " + value);  
        }  
        conn.commit();  
    } catch (SQLException e) {  
        e.printStackTrace ();  
    }  
  
    DbManager.getInstance().closeConnection(conn);  
}  
  
/**
 * Code example for writing and updating CLOB fields
 */  
public void writeClob() {  
    //Custom database connection management class   
    Connection conn = DbManager.getInstance().getConnection();  
    try {  
        conn.setAutoCommit(false);  
        // 1. This method can write to the CLOB field.  
        PreparedStatement stat = conn  
                .prepareStatement("insert into t_clob (id,clobfield) values(sys_guid(),?)");  
        String clobContent = "This is a very very long string";  
        StringReader reader = new StringReader(clobContent);  
        stat.setCharacterStream(1, reader, clobContent.length());  
        stat.executeUpdate();  
  
        // 2. Use a similar method to update the CLOB field, it will not succeed   
        // stat.close();  
        // stat =null;  
        // stat =  
        // conn.prepareStatement("update t_clob set clobfield=? where id=1");  
        // stat.setCharacterStream(1, reader, clobContent.length());  
        // stat.executeUpdate();  
  
        // 3. You need to use the for update method to update,  
        // However, it is important to note that if the original CLOB field has a value, you need to use empty_clob() to clear it.  
        // If the original is null, it cannot be updated, it must be the result returned by empty_clob().  
        stat = conn  
                .prepareStatement("select clobfield from t_clob where id='1' for update");  
        ResultSet rs = stat.executeQuery();  
        if (rs.next()) {  
            oracle.sql.CLOB clob = (oracle.sql.CLOB) rs  
                    .getClob("clobfield");  
            Writer outStream = clob.getCharacterOutputStream();  
            char[] c = clobContent.toCharArray();  
            outStream.write(c, 0, c.length);  
            outStream.flush();  
            outStream.close();  
        }  
        conn.commit();  
    } catch (SQLException | IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace ();  
    }  
    DbManager.getInstance().closeConnection(conn);  
}  

2. BufferedReader
        CLOB is used to store a large amount of character data. The JDBC code for reading CLOB is as follows.
Java Code Favorite Code
import java.sql.*;  
import java.io. *;  
  
public class ReadClob {  
    public static void main(String[] args) {  
        PreparedStatement pstmt = null;  
        ResultSet rset = null;  
        BufferedReader reader = null;  
        Connection conn = null;  
        String driver = "oracle.jdbc.driver.OracleDriver";  
        String strUrl = "jdbc:oracle:[email protected]:1521:ORCL";  
        try {  
            Class.forName(driver);  
            conn = DriverManager.getConnection(strUrl, "scott", "tiger");  
            pstmt = conn  
                    .prepareStatement("select v_clob form ord where ORD_id =?");  
            pstmt.setInt(1, 1);  
            rset = pstmt.executeQuery();  
            while (rset.next()) {  
                Clob clob = rset.getClob(1);// java.sql.Clob类型  
                reader = new BufferedReader(new InputStreamReader(clob  
                        .getAsciiStream()));  
                String line = null;  
                while ((line = reader.readLine()) != null) {  
                    System.out.println(line);  
                }  
            }  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace ();  
        } catch (SQLException e) {  
            e.printStackTrace ();  
        } catch (IOException e) {  
            e.printStackTrace ();  
        }  
    }  
}  

        The program code gets the CLOB field from the result set, and then converts it into an IO stream for reading. Then, the operation of JDBC writing a large amount of self-reading data into a CLOB field needs to first use the "for update" clause to find the CLOB field, and then obtain the CLOB field from the result set and convert it into the oracle.sql.CLOB type for writing.
Java Code Favorite Code
//write operation  
String stmtString = "select v_clob form ord  where ord_id =? for update";  
pstmt = conn.prepareStatement(stmtString);  
pstmt.setInt(1, 2);  
rset = pstmt.executeQuery();  
while(rset.next()){  
    //The shape is oracle.sql.CLOB  
    CLOB clob = (CLOB)rset.getClob(1);  
    String newClobDate = new String("NEW CLOOB DATE");  
    Writer writer = clob.getCharacterOutputStream();  
    //OutStream writer = clob.getAsciiOutputStream();  
    writer.write(newClobDate);  
}  

3. Generate a clob object, and achieve the purpose of inserting and updating through the preprocessed setClob.
        Method 1:
Java Code Collection Code
Connection con = dbl.loadConnection();  
strSql = "insert into table1(id,a) values (1,EMPTY_CLOB())";  
dbl.executeSql(strSql);  
String str2 = "select a from table1 where id=1";  
  
ResultSet rs = dbl.openResultSet(str2);  
if(rs.next()){  
    CLOB c = ((OracleResultSet)rs).getCLOB("a");  
    c.putString(1, "long string");  
    String sql = "update table1 set a=? where id=1";  
    PreparedStatement pstmt = con.prepareStatement(sql);  
    pstmt.setClob(1, c);  
    pstmt.executeUpdate();  
    pstmt.close();  
}  
con.commit ();  

        Method 2:
Java Code Collection Code
Connection con = dbl.loadConnection();  
CLOB clob   = oracle.sql.CLOB.createTemporary(con, false,oracle.sql.CLOB.DURATION_SESSION);  
clob.putString(1, "Long String");  
Sql1 = "update table1 set a=? where id=1";  
PreparedStatement pst = con.prepareStatement(Sql1);  
pst.setClob(1, clob);  
pst.executeUpdate();  
pst.close();  
con.commit ();  


2. sql method
        When a large number of strings (mainly html content) are directly inserted through the insert statement, and the number exceeds 4000 characters, it will report: ORA-01489: The result of string concatenation is too long.
        Although the field is a clob, which is enough for storage, when using this direct insertion, because the inserted string is not forced to be a clob type, oracle will treat the inserted string as a "string type", because oracle has the largest string Limit (not more than 4000 characters), so an error will be reported.
        Solution: Specify the type of the string to be inserted as clob, and you can use a procedure or a stored procedure.
Example:
Sql code favorite code
DECLARE  
 REALLYBIGTEXTSTRING CLOB := 'massive strings to be inserted';  
BEGIN  
   INSERT INTO test_table VALUES('test', REALLYBIGTEXTSTRING, '0');  
end ;  
/  
commit;

Supplement: Insert html content, which may contain spaces  , the character & is the keyword of Oracle, so it must be escaped before insertion, such as: '||chr(38)||'nbsp;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693556&siteId=291194637