Clob オブジェクトとは、SQL の CLOB (文字ラージオブジェクト) を Java プログラミング言語にマッピングした表現形式です。SQL の CLOB は、文字ラージオブジェクトをデータベース表の行内の列値として格納する組み込みの型です。プログラマは、ResultSet、CallableStatement、および PreparedStatement インタフェース内のメソッドを使用して、より基本的な SQL の型にアクセスするのと同じように SQL3 型 CLOB へアクセスできます。つまり、JDBC 2.0 API を使用するアプリケーションでは、CLOB 値に対する getClob および setClob などのメソッドが、INTEGER 値に対する getInt および setInt、CHAR 値または VARCHAR 値に対する getString および setString と同じように使用されます。
デフォルトでは、JDBC ドライバは、背後で SQL の LOCATOR(CLOB) 型を使用して Clob インタフェースを実装します。LOCATOR(CLOB) によってデータベースサーバ上の SQL の CLOB の値が指定され、ロケータ上の操作によって得られる結果は、CLOB 値自体での操作と同じです。つまり、クライアントマシン上で CLOB データを生成しなくても、クライアントは Clob インスタンス上で操作できます。LOCATOR(CLOB) は、ドライバによって背後で使用されるため、プログラマは、JDBC ドライバを完全に透過的に使用します。
Clob インスタンスの標準的な動作は、このインスタンスが作成されたトランザクションが、確定または元に戻るまで有効です。
Clob インタフェースには、SQL の CLOB 値を取得するメソッド、クライアント上で CLOB 値内にデータを生成するメソッド、および CLOB 値内で部分文字列または CLOB オブジェクトを検索するメソッドが用意されています。
次のコードは、Clob オブジェクトの作成方法を示しています。rs は、ResultSet オブジェクトです。
Clob clob = rs.getClob(1);
これで、変数 clob を使用して、結果セット rs の最初の列に格納された CLOB 値を操作できるようになります。
プログラマは、Clob オブジェクト上で、そのオブジェクトで指定した SQL CLOB 上で操作しているかのように、JDBC API 内のメソッドを呼び出すことができます。ただし、Clob オブジェクトを Java プログラミング言語内のオブジェクトと同じように操作する場合は、先にクライアント上で CLOB のデータを具体化する必要があります。Clob インタフェースには、Clob オブジェクトを Java プログラミング言語によるオブジェクトとして具体化する、3 つのメソッドが用意されています。
getAsciiStream は、CLOB の値を ASCII バイトを含むバイトストリームとして具体化する
Clob notes = rs.getClob("NOTES");
java.io.InputStream in = notes.getAsciiStream();
byte b = in.read();
// in contains the characters in the CLOB value designated by
// notes as Ascii bytes; b contains the first character as an Ascii
// byte
getCharacterStream は、CLOB の値を Unicode 文字のストリームとして具体化する
java.io.Reader reader = notes.getCharacterStream(); int c = Reader.read(); // c contains the first character in the CLOB that notes designates
getSubString は、CLOB の値のすべてまたは一部を String オブジェクトとして具体化する
String substring = notes.getSubString(10, 5); // substring contains five characters, starting with the tenth // character of the CLOB value that notes designates long len = notes.length(); String substring = notes.getSubString(1, len); // substring contains all of the characters in the CLOB object that // notes designates
Clob オブジェクトをデータベースに格納するには、PreparedStatement メソッドの setClob にパラメータとして Clob オブジェクトを渡します。たとえば次のコード例では、PreparedStatement オブジェクトの pstmt への最初の入力パラメータとして渡すことで、Clob オブジェクトの notes が格納されます。
Clob notes = rs.getClob("NOTES");
PreparedStatement pstmt = con.prepareStatement(
"UPDATE SALES_STATS SET COMMENTS = ?WHERE SALES > 500000");
pstmt.setClob(1, notes);
pstmt.executeUpdate();
notes が指定する CLOB の値は、SALES_STATS テーブル内で、SALES 列の値が 500000 より大きい場合に、各行の COMMENTS 列に格納されます。
package java.sql;
public interface java.sql.Clob {
long length() throws SQLException;
InputStream getAsciiStream() throws SQLException;
Reader getCharacterStream() throws SQLException;
String getSubString(long pos, int length) throws SQLException;
long position(String searchstr, long start) throws SQLException;
long position(Clob searchstr, long start) throws SQLException;
}
InputStream getAsciiStream() throws SQLException
このClobオブジェクトによって指定されるCLOB値を ASCII バイトのストリームとして生成します。この
Clobオブジェクトによって指定されるCLOB値の ASCII データが格納されたInputStreamオブジェクトjava.io.InputStream in = clob.getAsciiStream(); byte b = in.read(); // in has all of the characters in the CLOB value designated by // clob as Ascii bytes; b designates the first character as an Ascii // byte
Reader getCharacterStream() throws SQLException
このClobオブジェクトによって指定されるCLOB値を Unicode 文字のストリームとして具体化します。この
Clobオブジェクトによって指定されるCLOB値のすべてのデータを Unicode 文字として持つReaderオブジェクトReader read = clob.getCharacterStream(); // read has all the data in the CLOB value designated by clob // as Unicode characters
length getSubString(long pos, int length) throws SQLException
このClobオブジェクトによって指定されるCLOB値の、posの位置で始まり最高length個の連続した文字の部分のコピーを返します。
pos この Clobオブジェクトによって指定されるCLOB値から抽出される最初のcharの位置。初期位置は 1length コピーされる連続した文字数 この
Clobオブジェクトによって指定されるCLOB値の、posの位置からcharで始まる、連続した最大length文字のコピーを含むStringオブジェクトString substr = clob.getSubString(1, 100); // substr contains the first 100 characters in the CLOB value // designated by clob (those in positions 1 through 100, inclusive)
long length() throws SQLExceptions
このClobオブジェクトによって指定されるCLOB値の文字数を返します。この
Clobオブジェクトによって指定されるCLOB値の文字の長さClob clob = rs.getClob(3); long len = clob.length(); // len contains the number of characters in the CLOB value // designated by clob
long position(Clob searchstr, long start) throws SQLException
このClobオブジェクトによって指定されるCLOB値内で、Clobオブジェクトのsearchstrが開始する文字位置を決定します。検索は、startの位置から始まります。
searchstr 検索対象の Clobオブジェクトstart 検索を始める位置。最初の文字の位置は 1
Clobオブジェクトのsearchstrが始まる位置。startの位置で検索を始め、成功した場合はstart以上の値、それ以外の場合は -1 が返されます。Clob clob2 = rs.getClob(4); long beginning = clob.position(clob2, 1024); // if clob2 is contained in clob starting at position 1024 or later, // beginning will contain the position at which clob2 begins
long position(String searchstr, long start) throws SQLException
このClobオブジェクトによって指定されるCLOB値内で、Stringオブジェクトのsearchstrが開始する文字位置を決定します。検索は、startの位置から始まります。
searchstr 検索対象の文字列 start 検索を始める位置。最初の文字の位置は 1
Stringオブジェクトのsearchstrが始まる位置。startの位置で検索を始め、成功した場合はstart以上の値、それ以外の場合は -1 が返されます。String searchstr= clob.getSubString(5, 100); long beginning = clob.position(searchstr, 1024); // if searchstr is contained in clob from position 1024 on, beginning // will contain the position at which searchstr begins