Tag: Lob
Clobs in Oracle 10g
CLOB is the Oracle’s data type used for storing large textual data objects.
There is a problem when using standar APIs for processing CLOB data, it is that PreparedStatement.setString() only allows to process strings up to 32765 byte. To solve it, Oracle 10g has introduced a new property called SetBigStringTryClob that allows developers to use the standar JDBC PreparedStatement.setString() and ResultSet.getString() functions for also processing CLOB data.
Next snippet shows how to achieve CLOB handling by setting new property when creating the connection:
import java.sql.Connection;
import java.sql.DriverManager;
import oracle.jdbc.OracleDriver;
import java.util.Properties;
..........
// Load the database details into the variables.
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
// Create the properties object that holds all database details
Properties props = new Properties();
props.put("user", user );
props.put("password", password);
props.put("SetBigStringTryClob", "true");
// Load the Oracle JDBC driver class.
DriverManager.registerDriver(new OracleDriver());
// Get the database connection
Connection conn = DriverManager.getConnection( this.url, this.props );
This new property forces the preparedStatement.setString() to use another newly introduced method, OraclePreparedStatement.setStringForClob(), which can be used alone instead of the standar APIs. See how:
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;
..........
// Create SQL query to insert CLOB data and other columns in the database.
String sql = "INSERT INTO clob_tab VALUES(?)";
// Read a big file(larger than 32765 bytes).
// Note: method readFile() not listed here.
// It can be any method that reads a file.
String str = this.readFile("bigFile.txt");
// Create the OraclePreparedStatement object
opstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
// Use the new method to insert the CLOB data (for data greater or lesser than 32 KB)
opstmt.setStringForClob(1,str);
// Execute the OraclePreparedStatement
opstmt.executeUpdate();
...........
Taken from: Handling Clobs in Oracle 10g
I am Victor Santafé, a 24 years old guy currently living and working in Colombia. I am a System Engineer, I love my profession, I love everything about Software Engineering and Programming Languages, I love music, photography and everything related to arts.