Java Reference
In-Depth Information
How It Works
If your application requires the storage of string values, you need to know how large
those strings might possibly become. Most databases have an upper boundary when it
comes to the storage size of VARCHAR fields. For instance, the Oracle database has an
upper boundary of 2,000 characters and anything exceeding that length will be cut off.
If you have large amounts of text that need to be stored, use a CLOB field in the data-
base.
A CLOB is handled a bit differently from a string within Java code. In fact, it is ac-
tually a bit odd to work with the first couple of times you use it because you have to
create a CLOB from a Connection .
Note In reality, CLOB s and BLOB s (binary large objects) are not stored in the Oracle
table where they are defined. Instead, a large object ( LOB) locator is stored in the table
column. Oracle might place the CLOB in a separate file on the database server. When
Java creates the Clob object, it can be used to hold data for update to a specific LOB
location in the database or to retrieve the data from a specific LOB location within the
database.
Let's take a look at the loadClob() method contained in the solution to this re-
cipe. As you can see, a Clob object is created using the Connection cre-
ateClob() method. Once the Clob has been created, you set its contents using the
setString() method by passing the position indicating where to place the string
and the string of text itself:
textClob = conn.createClob();
textClob.setString(1, "This will be the recipe text in
clob format");
Once you have created and populated the Clob , you simply pass it to the database
using the PreparedStatement setClob() method. In the case of this example,
the PreparedStatement performs a database insert into the RECIPE_TEXT table
by calling the executeUpdate() method as usual.
Querying a Clob is fairly straightforward as well. As you can see in the
readClob() method that is contained within the solution to this recipe, a Pre-
paredStatement query is set up and the results are retrieved into a ResultSet .
Search WWH ::




Custom Search