Handling CLOBs - Made easy with Oracle JDBC 10g
Date: 17-Mar-2004
This document will help you understand:
|
• How to easily handle the CLOB data
at the JDBC layer using enhanced APIs available with Oracle JDBC
10g
• How to run the sample Java application demonstrating the
easy handling of CLOB data in the JDBC code.
|
Table of Contents
Introduction
Prerequisites
Software Requirements
How to Handle CLOBs Easily in
JDBC?
Running the Sample Java Application
Useful References
Introduction
JDBC (Java Database Connectivity) is a standard Java
interface for connecting from Java to relational databases. The JDBC
standard was defined by Sun Microsystems, allowing independent providers
to implement and extend the standard with their own.
In addition to supporting the standard JDBC API, Oracle drivers
have extensions to support Oracle-specific datatypes and to enhance
performance. For more information about Oracle JDBC, refer the Useful References
section in this document.
What are CLOBs?
Basically, LOBs (Large Objects) are designed to support
large unstructured data such as text, graphic images, still video clips,
full motion video, and sound waveforms. A typical employee record
may be a few hundred bytes, but even small amounts of multimedia data
can be thousands of times larger. Oracle supports the following two
types of LOBs:
-
Those stored in the database either in-line
in the table or in a separate segment or tablespace, such as BLOB(Binary
LOB), CLOB (Character LOB) and, NCLOB (National Character LOB).
As the name signifies, BLOB holds binary data while the CLOB
holds textual data and the NCLOB holds, character data that corresponds
to the national character set defined for the Oracle database.
-
Those stored as operating system files,
such as BFILEs.
Managing CLOBs and BLOBs in JDBC is not very simple
when compared to managing small amount of data. The way the CLOB and
the BLOB objects are created and managed is different than a VARCHAR(String)
column. In fact, Oracle extension classes are provided to support these
types objects in JDBC like oracle.sql.CLOB,
oracle.sql.BLOB
etc.
The new enhancements in Oracle JDBC 10g
promises
to simplify the insertion and retrieval techniques for the CLOBs using
the standard APIs
. This document enumerates how easily the CLOB
data can be handled in JDBC, by harnessing the new features of the
Oracle JDBC 10g
driver.
Prerequisites
To work your way through this HowTo, it is necessary
to have a basic understanding of the fundamental concepts of JDBC,
and how to use the JDBC APIs to connect to the database for the basic
CRUD(Create, Read, Update, and Delete)- the essential database operations.
In addition, you need to know the basic Oracle database concepts.
Software Requirements
-
Oracle
Database
10g
. You can download the database here
.
-
Oracle Database 10g
JDBC Driver. This
can be downloaded here
.
- JDK1.4.x or later that can be downloaded
here
.
How To Handle CLOBs Easily in JDBC?
Prior to Oracle JDBC 10g
, to manipulate the
CLOB data in JDBC, Oracle extension class oracle.sql.CLOB
was used. But now, Oracle JDBC 10g
has a few enhancements that simplifies
the CLOB manipulation in JDBC applications. This enables handling of
large data using some of the available standard APIs
, instead
of using the Oracle extension classes. These could be thought as shortcuts
for inserting and retrieving CLOB data from the database.
The enhanced standard APIs for manipulating CLOBs are
the setString()
and getString()
of the PreparedStatement
and ResultSet
objects respectively. By
default, the method preparedStatement.setString()
will allow processing of the strings up to 32765 bytes. In order to
insert data greater than 32765 bytes, a newly introduced Connection
property -
SetBigStringTryClob
can be set. This forces the preparedStatement.setString()
to use another
newly introduced method, OraclePreparedStatement.setStringForClob()
instead. This is actually done internally, transparent to the user.
Nevertheless, the newly introduced method OraclePreparedStatement.setStringForClob()
alone can also be used instead of the standard APIs. This method makes
the check on the data size internally again.
ResultSet.getString()
can still be
used to read the CLOB column. For the getString()
and the setString()
operations, the size
limit for the string to be read or inserted is the one imposed by Java,
that is, a positive int; the smallest being 0 or 1 byte.
Note: Do not use the setString()
to bind large data to VARCHAR and LONG database columns, since it may
truncate the data or cause errors.
In summary, PreparedStatement.setString()
comes handy for processing the CLOB data, by just setting the Connection
property SetBigStringTryClob
.
However, handling very large amounts of data this way may not be a
wise; streaming the data is a better alternative.
Following is the code snippet to set the Connection
property to process large data
using the standard APIs. Refer the full source code in the file:
ClobManipulationIn10g.java
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 );
|
The code snippet to create an Oracle database table
with a CLOB column in it.
// SQL statement
CREATE TABLE clob_tab (clob_col CLOB);
|
Once the Connection
property - SetBigStringTryClob
is set,
use the standard preparedStatement.setString()
method for binding data more than 32765 bytes.
PreparedStatement pstmt = conn.prepareStatement(
"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");
// The string data is automatically transformed into a CLOB and
// inserted into the database column.
// Make sure that the Connection property - 'SetBigStringTryClob' is
// set to true for the insert to happen.
pstmt.setString(1, str);
pstmt.executeUpdate();
|
Instead of the standard APIs, Oracle extension
APIs can be used. OraclePreparedStatement.setStringForClob()
can be used for binding data greater than 32765 bytes.
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();
...........
|
Following is the code snippet that demonstrates the
use of the standard ResultSet.getString()
method, enhanced now to read more than 32765 bytes:
.....
// Create a PreparedStatement object
PreparedStatement pstmt = null;
// Create a ResultSet to hold the records retrieved.
ResultSet rset = null;
.......
// Create SQL query statement to retrieve records having CLOB data from
// the database.
String sqlCall = "SELECT clob_col FROM clob_tab";
pstmt= conn.prepareStatement(sqlCall);
// Execute the PrepareStatement
rset = pstmt.executeQuery();
String clobVal = null;
// Get the CLOB value larger than 32765 bytes from the resultset
while (rset.next()) {
clobVal = rset.getString(1);
System.out.println("CLOB length: "+clobVal.length());
}
|
Running the Sample Java Application
- Copy the sample Class file
ClobManipulationIn10g.java
in a directory, say
for example, clobs
.
-
From the
clobs
directory, set the environment variable CLASSPATH to include the following:
-
Include the JDBC driver class; use
ojdbc14.jar
with JDK 1.4.x / use classes12.jar/zip
with JDK 1.3.x
-
Include the current directory(.
)
in the CLASSPATH.
-
Create the table 'clob_tab' in an Oracle database
under a user say, scott. Refer the code snippet
for more details.
-
Edit the file
ClobManipulationIn10g.java
to configure your database details.
- Save the file. Compile the Java file using:
javac -d . ClobManipulationIn10g.java
- Run the class using the following command.
Pass a file name with the complete path as the parameter value for
<fileName>. For testing, use a file which is >32 KB of size.
java oracle.otnsamples.jdbc.ClobManipulationIn10g
"<fileName>"
For example, java oracle.otnsamples.jdbc.ClobManipulationIn10g
"d:\test\bigFile.txt"
Depending on the size of the file(say for example, 40kb) that
was read, the output will be like the following:
CLOB length:
40000
orginal site:http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/handlingclobsinoraclejdbc10g.html
分享到:
相关推荐
### Oracle Database 10g PL/SQL Programming #### 1. Introduction to PL/SQL PL/SQL (Procedural Language for SQL) is a procedural extension to SQL that provides a powerful way to manipulate data within...
标题“sqljdbc_1.2”指的是SQL Server JDBC驱动程序的一个特定版本,1.2版。JDBC(Java Database Connectivity)是Java编程语言中用于与各种数据库交互的标准API。这个驱动程序是由Microsoft开发的,允许Java应用...
Oracle OCCI(Oracle C++ Call Interface)是Oracle数据库提供的一个编程接口,它允许C++开发者通过C++程序与Oracle数据库进行交互。OCCI为开发人员提供了一种高效、灵活的方式来构建数据库相关的应用程序,特别是...
根据给定文件的信息,我们可以深入探讨Oracle数据库及SQL快速参考指南的关键知识点,这些知识点主要集中在Oracle Database SQL Quick Reference,版本为10g Release 1(10.1)上。 ### Oracle数据库概述 Oracle...
### Oracle 11g R2 OCI (Oracle Call Interface) 知识点详解 #### 一、OCI 概述 **Oracle Call Interface (OCI)** 是一个应用程序接口 (API),用于开发高性能的应用程序,这些应用程序可以利用 Oracle 数据库的...
当涉及到处理大型对象(LOBs)如Blobs(Binary Large Objects)和Clobs(Character Large Objects)时,JDBC4提供了一种更为便捷的方式来操作这些数据类型。这篇博客“使用Jdbc4操作Blob,Clob”将深入讲解如何利用...
Oracle Instant Client是Oracle公司提供的一款轻量级的数据库连接工具,用于在不安装完整Oracle数据库客户端的情况下,允许应用程序连接到远程Oracle数据库服务器。在给定的压缩包"Instantclient-sqlplus-windows.x...
ODAC(Oracle Data Access Components)是专门为Delphi和C++Builder开发者设计的一套全面的数据库访问组件,用于简化Oracle数据库的编程工作。这套组件库提供了高效、强大的工具,使开发人员能够快速创建与Oracle...
2. **全面支持Oracle特性**:ODAC支持Oracle的各种高级特性,如PL/SQL过程、触发器、游标、BLOBs、CLOBs、NCLOBs、BFILEs等。此外,它还支持Oracle的分区表、物化视图、索引组织表等高级数据库结构。 3. **组件集**...
2. **广泛的数据类型支持**:Ocilib不仅涵盖了基本的数据类型,如整数、浮点数、字符串等,还支持Oracle的复杂数据类型,如BLOBs、CLOBs、DATEs、TIMESTAMPs等。 3. **自动内存管理**:Ocilib使用智能指针和自动...
Oracle数据库支持多种数据类型,包括数值、字符串、日期时间以及复杂的数据结构,如BLOBs(Binary Large OBjects)和CLOBs(Character Large OBjects)。 DML(Data Manipulation Language)是Oracle数据库中用于...
Oracle Call Interface (OCI) 是 Oracle 数据库提供的一种 C 语言编程接口,允许开发者直接与数据库进行交互,实现高效、低级别的数据库操作。"OCI程序员参考手册9i"是针对 Oracle 9i 数据库版本的详细指南,对于...
You can now filter on multiple comma-separated object names (e.g. “dept%, emp%”). You can now refresh materialized views from the popup menu. File Browser enhancements Git and Subversion support ...
64位版本的 PLSQL 正式版,只能运行在64位系统中,需要你安装 64 位的 Oracle 客户端。 安装请查看说明。 APRIL 17, 2020 - VERSION 14.0 RELEASED Built-in Version Control support for Git and Subversion ...
- MySQL Connector/ODBC 支持多种特性,包括事务处理、预编译语句、SSL 加密、大对象(BLOBs 和 CLOBs)以及多种字符集支持。 2. **版本 5.3.11**: - 这是 MySQL Connector/ODBC 的一个稳定版本,提供了许多性能...
其次,书中详细讲解了如何在Oracle中处理复杂的数据类型,如BLOBs(二进制大对象)和CLOBs(字符大对象),以及如何使用日期和时间数据类型。这涵盖了数据类型的选择、存储和查询,以及与这些类型相关的函数和操作。...
介绍了如何处理大对象(BLOBs 和 CLOBs)。 - **3.4.4 二级缓存和查询缓存** 讲解了 Hibernate 中的缓存机制。 - **3.4.5 替换查询语言** 介绍了如何自定义 Hibernate 查询语言。 - **3.4.6 Hibernate 统计...
同时,Java 6还更新了JDBC 4.0,支持将XML作为SQL类型,改进了对二进制大对象(BLOBs)和字符大对象(CLOBs)的支持,使得数据处理更加高效和便捷。 #### 4. More Desktop APIs 为了改善桌面应用开发体验,Java 6...
- **Clobs表**:用于存储大型文本数据。 - **索引枢轴表**:存储索引信息,提高查询性能。 - **唯一字段枢轴表**:确保字段值的唯一性。 - **关系枢轴表**:定义不同对象之间的关联关系。 - **回退索引表**:提供...