|
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
public class COLUpdate {
public static void main(String args[]) throws SQLException {
Connection con = null;
Statement stm = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL","test","test");
// 自動コミットを無効にする。
con.setAutoCommit(false);
stm = con.createStatement();
// 新規レコードをinsert。blod_dataは、empty_clob()を使用して初期化する。
stm.executeUpdate("insert into clob_sample values('C01',empty_clob())");
// insertしたレコードをselectする。ロックを掛けるので、for updateを使用する。
rs = stm.executeQuery(
"select clob_id,clob_data from clob_sample where clob_id = 'C01' for update");
rs.next();
// 列「clob_data」のロケータからOutputStreamを取得する。
oracle.sql.CLOB bmp = ((OracleResultSet)rs).getCLOB("clob_data");
Writer outStream = bmp.getCharacterOutputStream();
// ラージテキストデータサイズを取得する
int size = bmp.getBufferSize();
char [] buffer = new char[size];
// DBにinsertするラージテキストファイルをオープン
File f = new File("TEST.TXT");
FileReader inStream = new FileReader(f);
int length = -1;
while ((length = inStream.read(buffer)) != -1) {
// 読み込んだラージテキストファイルを列「clob_data」に書き込む
outStream.write(buffer,0,length);
}
inStream.close();
outStream.close();
// コミット
con.commit();
} catch (Exception ex) {
ex.printStackTrace();
con.rollback();
} finally {
if ( rs != null ) rs.close();
if ( stm != null ) stm.close();
if ( con != null ) con.close();
}
}
} |