some problem with CLOB

R

relogout

I have a oracle table with a CLOB field named NEWS_CONTETN, and I want
put some news information which may be larger than 4k, I have searched
for some example and take following steps:

1. insert an empty clob to the table
sql1="insert into TAB1(id,COL_CLOB) values(1,empty_clob())";

2. initialize a CLOB variable and select the clob field for update

sql2="select COL_CLOB from TAB1 where id=1 for update";
oracle.sql.CLOB clob=null;
if(rs.next()){
clob=(oracle.sql.CLOB)rs.getClob("COL_CLOB");
}


3. put the news information to the CLOB variable
===================
HERE IS MY PROBLEM
in step 3, i have tried 3 ways, but it didn't work :(
===================
(1) clob.putChars(1,newsStr.toString().toCharArray()); //newsStr is
a string
(2) java.io_OutputStream out = clob.setAsciiStream(0);
(3) Writer outStream = clob.getCharacterOutputStream();

4. update the table
sql3="update TAB1 set COL_CLOB=? where id='1'";
 
R

relogout

(e-mail address removed), 10.09.2008 04:35:










First of all: make sure you use an Oracle 10.x driver. The Oracle 9.x drivers do not support LOBs in a decent fashion.

1) Insert (or update) a CLOB

String sql = "insert into tab1 (col_clob) values (?)";
String myLongClobContent = " ... whatever ...";
StringReader r = new StringReader(myLongClobContent);
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setCharacterStream(1, r, myLongClobContent.length());
stmt.executeUpdate();
connection.commit();

2) to read a CLOB

String sql = "SELECT col_clob FROM tyb1";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
{
  Reader in = rs.getCharacterStream(1);
  // read from the stream into a String variable

  // or simply use getString(1), I think that is supported by newer drivers as well.

}

Again: make sure your driver version is at least 10.1

Thomas

Thank you very much, Thomas! I use setCharacterStream() instead, and
it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top