aspose file tools*
The moose likes JDBC and the fly likes How to update clob field in a table using jdbc? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to update clob field in a table using jdbc?" Watch "How to update clob field in a table using jdbc?" New topic
Author

How to update clob field in a table using jdbc?

Mainak Sikdar
Greenhorn

Joined: Apr 15, 2012
Posts: 6
Can anyone please tell me the process of updating clob field in a table using jdbc??I need it urgently.Someone please help me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mainak Sikdar
Greenhorn

Joined: Apr 15, 2012
Posts: 6
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.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Apr 15, 2012
Posts: 6
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

Joined: Apr 15, 2012
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to update clob field in a table using jdbc?