Ing of SQL CLOB in JavaTM programming language. SQLCLOB is a built-in type that stores CharacterLargeObject as a column in a row of a database table. By default, the driver uses SQLlocator (CLOB) to implement the Clob object, which means that the CLOB object contains a data pointing to SQLCLOB instead
Ing of SQL CLOB in Java TM programming language. SQL CLOB is a built-in type that stores Character Large Object as a column in a row of a database table. By default, the driver uses SQL locator (CLOB) to implement the Clob object, which means that the CLOB object contains a data pointing to SQL CLOB instead
SQL typeCLOB
Ing relationships in the JavaTM programming language. SQLCLOB
Is a built-in type, which stores Character Large Object as a column value in a row of the database table. By default, the driver uses SQLlocator(CLOB)
ImplementationClob
Object, which meansCLOB
The object containsCLOB
Data is not the logical pointer of the data itself.Clob
The object is valid during the transaction processing it creates.
CLOB
Interface provides some methods to obtain SQLCLOB
(Character Large Object) value length, implemented on the clientCLOB
Value and search for a substring orCLOB
ValueCLOB
Object. InterfaceResultSet
,CallableStatement
AndPreparedStatement
(For examplegetClob
AndsetClob
) Allow programmers to access SQLCLOB
Value. In addition, this interface also has updatesCLOB
Value method.
In Oracle, Varchar2 supports a maximum of 4 kb. Therefore, for processing long strings, we need to use CLOB fields. CLOB fields support a maximum of 4 GB.
There are several other types:
Blob: binary, if exe, zip
Clob: single-byte code, such as a common text file.
Nlob: Multi-byte code, such as UTF files.
The following describes how to operate the CLOG field. It is used in the help documentation section of our project.
1. Write Data first
Java code
- /* In the following table, when the HCONTENT field in PF_HELP_CONTENT is of the CLOB type */
- // Generate the help ID through the sequencer
- Map map = Query. getMap ("Select TO_CHAR (SEQ_HID.nextval) hid from dual ");
- Hid = String. valueOf (map. get ("HID "));
- // Insert a data record. Note that the CLOB field requires an empty clob type empty_clob () first, and then update the clob field separately.
- SQL = "Insert INTO PF_HELP_CONTENT (HID, HCONTENT) VALUES (?, Empty_clob ())";
- Try
- {
- // Execute insert
- Rtn = DbUtils.exe cuteUpdate (SQL, hid );
- /* After successful insertion, modify the content of the HCONTENT field */
- // Obtain the database connection
- Connection conn = DbUtils. getConnection ();
- // Manually submit
- Conn. setAutoCommit (false );
- // Define the ResultSet and Clob Variables
- ResultSet rs = null;
- Oracle. SQL. CLOB clob = null;
- // Update SQL
- String sqlclob = "Select hcontent from PF_HELP_CONTENT Where HID =? FOR Update ";
- Java. SQL. PreparedStatement pstmt = conn. prepareStatement (sqlclob );
- // Hid is of the varchar2 type, so setString is used.
- Pstmt. setString (1, hid );
- // Execute the update statement
- Rs = pstmt.exe cuteQuery ();
- If (rs. next ())
- {
- // Obtain the content of the preceding HCONTENT, that is, the added empty_clob ()
- Clob = (oracle. SQL. CLOB) rs. getClob (1 );
- }
- // The output must be in clob. getCharacterOutputStream () stream mode.
- Writer write = clob. getCharacterOutputStream ();
- // Write the specific content. helpform. getHContent () stores the Help content
- Write. write (helpform. getHContent ());
- Write. flush ();
- Write. close ();
- Rs. close ();
- // Submit
- Conn. commit ();
- Conn. close ();
- }
- Catch (Exception ex)
- {
- //.........
- }
Java code
- /* In the following table, when the HCONTENT field in PF_HELP_CONTENT is of the CLOB type */
- // Generate the help ID through the sequencer
- Map map = Query. getMap ("Select TO_CHAR (SEQ_HID.nextval) hid from dual ");
- Hid = String. valueOf (map. get ("HID "));
- // Insert a data record. Note that the CLOB field requires an empty clob type empty_clob () first, and then update the clob field separately.
- SQL = "Insert INTO PF_HELP_CONTENT (HID, HCONTENT) VALUES (?, Empty_clob ())";
- Try
- {
- // Execute insert
- Rtn = DbUtils.exe cuteUpdate (SQL, hid );
- /* After successful insertion, modify the content of the HCONTENT field */
- // Obtain the database connection
- Connection conn = DbUtils. getConnection ();
- // Manually submit
- Conn. setAutoCommit (false );
- // Define the ResultSet and Clob Variables
- ResultSet rs = null;
- Oracle. SQL. CLOB clob = null;
- // Update SQL
- String sqlclob = "Select hcontent from PF_HELP_CONTENT Where HID =? FOR Update ";
- Java. SQL. PreparedStatement pstmt = conn. prepareStatement (sqlclob );
- // Hid is of the varchar2 type, so setString is used.
- Pstmt. setString (1, hid );
- // Execute the update statement
- Rs = pstmt.exe cuteQuery ();
- If (rs. next ())
- {
- // Obtain the content of the preceding HCONTENT, that is, the added empty_clob ()
- Clob = (oracle. SQL. CLOB) rs. getClob (1 );
- }
- // The output must be in clob. getCharacterOutputStream () stream mode.
- Writer write = clob. getCharacterOutputStream ();
- // Write the specific content. helpform. getHContent () stores the Help content
- Write. write (helpform. getHContent ());
- Write. flush ();
- Write. close ();
- Rs. close ();
- // Submit
- Conn. commit ();
- Conn. close ();
- }
- Catch (Exception ex)
- {
- //.........
- }
2. Modify the CLOB field content
Java code
- /* The modification is basically the same as the insert operation. It is also implemented using for update */
- // If the length of the field before modification is greater than the length of the current modification, the content at the end will still exist
- // Set the content of PF_HELP_CONTENT to null before modifying the content
- SQL = "Update PF_HELP_CONTENT SET HCONTENT = empty_clob () Where HID =? ";
- Try
- {
- Rtn = DbUtils.exe cuteUpdate (SQL, hid );
- // The following operations are the same as when adding
- Connection conn = DbUtils. getConnection ();
- Conn. setAutoCommit (false );
- ResultSet rs = null;
- Oracle. SQL. CLOB clob = null;
- String sqlclob = "Select hcontent from PF_HELP_CONTENT Where HID =? FOR Update ";
- Java. SQL. PreparedStatement pstmt = conn. prepareStatement (sqlclob );
- Pstmt. setString (1, hid );
- Rs = pstmt.exe cuteQuery ();
- If (rs. next ())
- {
- Clob = (oracle. SQL. CLOB) rs. getClob (1 );
- }
- Writer write = clob. getCharacterOutputStream ();
- Write. write (helpform. getHContent ());
- Write. flush ();
- Write. close ();
- Rs. close ();
- Conn. commit ();
- Conn. close ();
- }
- Catch (Exception ex)
- {
- //...
- }
Java code
- /* The modification is basically the same as the insert operation. It is also implemented using for update */
- // If the length of the field before modification is greater than the length of the current modification, the content at the end will still exist
- // Set the content of PF_HELP_CONTENT to null before modifying the content
- SQL = "Update PF_HELP_CONTENT SET HCONTENT = empty_clob () Where HID =? ";
- Try
- {
- Rtn = DbUtils.exe cuteUpdate (SQL, hid );
- // The following operations are the same as when adding
- Connection conn = DbUtils. getConnection ();
- Conn. setAutoCommit (false );
- ResultSet rs = null;
- Oracle. SQL. CLOB clob = null;
- String sqlclob = "Select hcontent from PF_HELP_CONTENT Where HID =? FOR Update ";
- Java. SQL. PreparedStatement pstmt = conn. prepareStatement (sqlclob );
- Pstmt. setString (1, hid );
- Rs = pstmt.exe cuteQuery ();
- If (rs. next ())
- {
- Clob = (oracle. SQL. CLOB) rs. getClob (1 );
- }
- Writer write = clob. getCharacterOutputStream ();
- Write. write (helpform. getHContent ());
- Write. flush ();
- Write. close ();
- Rs. close ();
- Conn. commit ();
- Conn. close ();
- }
- Catch (Exception ex)
- {
- //...
- }
3. Retrieve the text content of the CLOB Field
Java code
- /* The previous parts are consistent */
- Connection conn = DbUtils. getConnection ();
- Conn. setAutoCommit (false );
- ResultSet rs = null;
- Oracle. SQL. CLOB clob = null;
- String sqlclob = "Select hcontent from PF_HELP_CONTENT Where HID =? ";
- Java. SQL. PreparedStatement pstmt = conn. prepareStatement (sqlclob );
- Pstmt. setString (1, hid );
- Rs = pstmt.exe cuteQuery ();
- If (rs. next ())
- {
- // Parameter 1 in rs. getClob (1) indicates the HCONTENT field index. The first field starts from 1 rather than from 0.
- // You can also use the field name to obtain rs. getClob ("HCONTENT ")
- Clob = (oracle. SQL. CLOB) rs. getClob (1 );
- }
- If (clob = null | clob. length () = 0)
- {
- Hcontent = "";
- } Else
- {
- // Take the CLOB field content as a string
- Hcontent = clob. getSubString (long) 1, (int) clob. length ());
- }
- Rs. close ();
- Conn. close ();
- Request. setAttribute ("HCONTENT", hcontent );