• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to update clob field in a table using jdbc?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me the process of updating clob field in a table using jdbc??I need it urgently.Someone please help me.
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mainak Sikdar wrote:I need it urgently.


Please EaseUp. There is no such thing as "urgent" around here.

You should check out PreparedStatement. You can use a parameter (?) for the field, then use one of the many set methods to specify the value.
 
Mainak Sikdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clob clob = null;
try
{
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT CLOB FROM YOUR_TABLE WHERE ID =" + rowID + " FOR UPDATE OF CLOB");
if(rs.next())
{
clob = rs.getClob("CLOB");
}

OutputStream os = ((oracle.sql.CLOB) clob).getAsciiOutputStream();

byte[] b = updateString.getBytes("ASCII");

os.write(b);
os.flush();
os.close();

pstmt = con.prepareStatement("UPDATE YOUR_TABLE SET CLOB = ? WHERE ID = " + rowID);

pstmt.setClob(1, clob);
pstmt.executeUpdate();


I am using the above code.But it does not change anything in the clob field of oracle.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read documentation for your oracle version ?

Here is a link for oracle 11g:
http://docs.oracle.com/cd/B28359_01/java.111/b31224/oralob.htm#g1070326


Example: Writing CLOB Data

Use the setCharacterStream method or the setAsciiStream method to write data to a CLOB. The setCharacterStream method returns a Unicode output stream. The setAsciiStream method returns an ASCII output stream.

The following example reads a vector of data into a character array, then uses the setCharacterStream method to write the array of character data to a CLOB.

java.io.Writer writer;

// read data into a character array
char[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of character data to a Clob
writer = ((CLOB)my_clob).setCharacterStream();
writer.write(data);
writer.flush();
writer.close();
...

The next example reads a vector of data into a byte array, then uses the setAsciiStream method to write the array of ASCII data to a CLOB.

java.io.OutputStream out;

// read data into a byte array
byte[] data = {'0','1','2','3','4','5','6','7','8','9'};

// write the array of ascii data to a CLOB
out = clob.setAsciiStream();
out.write(data);
out.flush();
out.close();



 
Mainak Sikdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used setCharacterStrem() as you said above.But it is giving me a error.I am getting the following error-

The method setCharacterStream(long) in the type CLOB is not applicable for the arguments ().Please help.
 
Mainak Sikdar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally found the solution.Here's the code that helped me....
StringReader clob = new StringReader("");
pStmt = conn.prepareStatement("update activity set REFERENCE = ? WHERE ID = ?");
pStmt.setCharacterStream(1, clob, 0);
pStmt.setLong(2, 1);
pStmt.executeUpdate();
Here in pstmt.setCharacterStream() the last parameter is the length of the clob data with which I want to update.Thanks everybody for giving a look at my code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic