Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: CLOB fields
Frederik Hansen schrieb in Nachricht <37E8E45C.C4977B7B_at_post1.tele.dk>...
>Hi
>
>How can I write a CLOB to the database? Or more needed how can I get a
>CLOB object to where I can write my data. I have done this:
> byte[] bytes = { '1','2' };
>
> java.io.OutputStream out;
> oracle.sql.CLOB cl;
>
> if (bytes.length == 0)
> {
> // **********
> cl = new oracle.sql.CLOB((oracle.jdbc.driver.OracleConnection)conn);
> // ********** ^ This is my problem...
>
> out = cl.getAsciiOutputStream();
> try
> {
> out.write(bytes);
> out.flush();
> out.close();
> }
> catch (IOException e)
> {
> e.printStackTrace();
>
> throw new SQLCORBAException(0, "Error writing CLOB socket");
> }
>
> psta.setClob(1, cl);
> }
> else
> psta.setNull(1, java.sql.Types.CLOB);
>
>Any one know who to do this??
>
>Frederik Hansen
Hi your idea has only one error:
if you want insert a clob, you have to use an existing clob - seems nasty
but as far as I know its the only way:
here is the code example - I've cutted it out from a running project- hope
copied all, but you can clearly see the idea.
the table
create table atable /id number, media CLOB;
create the clob-field:
String cmd = "Insert Into atable Values ('1','notext')"; OraclePreparedStatement pstmt = (OraclePreparedStatement) ; connection.prepareStatement(cmd); pstmt.execute(); pstmt.close();
now you can use it:
try{ Statement stmt = connection.createStatement(); ResultSet rset = stmt.executeQuery ("SELECT MEDIA FROM WHERE MEDIA_ID =1") if (rset.next ()) CLOB clob = ((OracleResultSet)rset).getCLOB (1);
we've got it - lets fill it
URL textFile = new URL('some existing file'); BufferedInputStream bis = (BufferedInputStream) file.openStream(); URLConnection urlc = file.openConnection();
BufferedInputStream bis = (BufferedInputStream) urlc.getContent(); Writer outstream = clob.getCharacterOutputStream();
int b;
while ((b = bis.read()) != -1 ){
outstream.write(b);
}
bis.close();
outstream.close();
}
rset.close();
stmt.close(); Received on Wed Sep 22 1999 - 14:23:37 CDT
![]() |
![]() |