RSingh
RSingh

Reputation: 171

Assign value to clob variable

Let's assume there is a variable CLOB response. How do i assign some value to this clob variable in java?

I am doing this:

Clob clob=null;
clob.setString(0, "<test></test>");

The second line is showing an error saying clob could be only null here.

update-

ValResponses valResponses=new ValResponses();
          valResponses.setVraId(Long.parseLong("12"));
          Clob clob = null;
          //clob.setString(1, "<test></test>");
          valResponses.setFullResponse(clob);
          valResponses.setValReport(fileName);

See there is one pojo ValResponses now this pojo has one variable valresponse having type CLOB,now I want to set some dummy value in that how to do that,?

Upvotes: 1

Views: 9563

Answers (4)

user4910279
user4910279

Reputation:

Define a class implements java.sql.Clob as:

public class MyClob implements java.sql.Clob {
    final String value;
    public MyClob(String value) { this.value = value; }
    @Override public long length() { return value.length(); }
    @Override public String getSubString(long pos, int length) {
        return value.substring((int)pos, (int)pos + length);
    }
    @Override public Reader getCharacterStream() { return new StringReader(value); }
    @Override public InputStream getAsciiStream() { return null; } 
    @Override public long position(String searchstr, long start) { return 0; }
    @Override public long position(Clob searchstr, long start) { return 0; }
    @Override public int setString(long pos, String str) { return 0; } 
    @Override public int setString(long pos, String str, int offset, int len) { return 0; } 
    @Override public OutputStream setAsciiStream(long pos) { return null; } 
    @Override public Writer setCharacterStream(long pos) { return null; } 
    @Override public void truncate(long len) { } 
    @Override public void free() { } 
    @Override public Reader getCharacterStream(long pos, long length) { return null; }

}

And then

Clob clob = new MyClob("<test></test>");

Upvotes: 0

javapointdotcom
javapointdotcom

Reputation: 35

You can also try using the simple approach to create the Clob

Clob xmlClob = con.createClob();

Upvotes: 0

Arjit
Arjit

Reputation: 3456

Get it from the connectionobject, then set the value in to it.

OracleConnection conn;  // initialize this first

CLOB clob = conn.CreateClob();
clob.setString(0, "<test></test>");

Or, another way to set the value

Clob myClob = null;
if (rs.next()) { //rs is ResultSet object
      myClob = rs.getClob(1);
      System.out.println("Length of retrieved Clob: "+myClob.length());
 }

Upvotes: 0

javapointdotcom
javapointdotcom

Reputation: 35

First, initialize the clob value like below an set the value(xml) to it

java.sql.Clob clobValue = oracle.sql.CLOB.createTemporary(dBconnection, false, oracle.sql.CLOB.DURATION_SESSION);

Upvotes: 0

Related Questions