Converting from String to Clob using setString() not working

how to convert clob to string in java
java set clob example
clob createtemporary

I am trying to convert a String into a Clob to store in a database. I have the following code:

Clob clob = connection.createClob();
System.out.println("clob before setting: " + clob);
clob.setString(1,"Test string" );
System.out.println("clob after setting: " + clob);
System.out.println("clob back to string: " + clob.toString());

When I run this the Clob is not being set, the output is as follows:

clob before setting: org.apache.derby.impl.jdbc.EmbedClob@1f5483e
clob after setting: org.apache.derby.impl.jdbc.EmbedClob@1f5483e
clob back to string: org.apache.derby.impl.jdbc.EmbedClob@1f5483e

Everywhere I look says to use the setString method, I have no idea why this isn't working for me.

You don't need the intermediate Clob instance, just use setString() on the PreparedStatement:

PreparedStatement stmt = connection.prepareStatement("insert into clob_table (id, clob_column) values (?,?)";
stmt.setInt(1, 42);
stmt.setString(2, "This is the CLOB");
stmt.executeUpdate();
connection.commit();

Converting from String to Clob using setString() not working, Converting from String to Clob using setString() not working. Question. I am trying to convert a String into a Clob to store in a database. I have the following code: public static void EditEvent(string EVENTID, string eventtext, string active,OracleClob content, string topnewalertpic, string file, string topimageevent) i am declaring my function like this but in asp i am not able to find out how to declare CLOB datatype, i tried OracleClob but its not working.

Not sure if it works for derby, but for hibernate you can use:

public Clob createClob(org.hibernate.Session s, String text) {
    return s.getLobHelper().createClob(text);
}

String to CLOB conversion (JDBC and Relational Databases forum , XML is in StringBuffer and i need to convert that in clob. AbstractMethodError: int java.sql.Clob.setString(long, java.lang. There are other methods in Clob, how about the setCharacterStream() call to get a Writer you can write the String to​? The sample code mentioned around does not work for me. ∟ Oracle CLOB (Character Large Object) - TEXT ∟ Inserting CLOB Values with setString() Method. This section describes how to insert CLOB values with the PreparedStatement.setString() method.  Another way to insert a character string into a CLOB column is to create a PreparedStatement object and use the setString() method. See the sample

What you are reading from your System.out.println statements are not indicative of what is actually happening in the Clob. You are simply reading out the default java.lang.Object.toString() method of the Clob, which in this case is outputting the instance ID, and that does not change no matter what you put in it.

You are using the Clob properly to load the String on to it, but not read the String value back. To read the String value from a clob use...

Clob clob = connection.createClob();
clob.setString(1,"Test string" );
String clobString = clob.getSubString(1, clob.length())
System.out.println(clobString);

BTW: if you are using large strings, you DO want to convert your String to a Clob, if the String is larger than the Oracle varchar2 limit and you send a simple String it could truncate the input or blow up. If the Oracle proc calls for a Clob, give it one.

java.sql.Clob.setString java code examples, Best Java code snippets using java.sql.Clob.setString (Showing top 20 results "​Copied string into temporary CLOB with length " + content.length() : "Set CLOB  William Brogden wrote:Since java.sql.Clob does not have a method for dealing with XML Document objects, it appears to me that you will have to transform the Document to a stream of characters, or otherwise convert it to a String to use one of the Clob methods. can you please provide the help in conversition of String to java.sql.Clob

14 Working with LOBs and BFILEs, Getting and Passing BLOB and CLOB Locators an int column index or a String column name as input. You can use getBLOB() and getCLOB() , which return oracle.sql. Datum object, and cast the output appropriately. BFILEs are not transaction controlled. create clob from string for Oracle oracle.sql.CLOB tempclob = oracle.sql.CLOB.createTemporary( // the connection used to // create the clob // has to be the same as is

Oracle cast as clob, At work I am building an interface using SQL object types and SQL collection types. It is overloaded for data types RAW, BLOB and CLOB, but not for VARCHAR2. In Oracle, EMPTY_CLOB() function allows you to assign an empty string (with with setString(), setCharacterStream() or setClob() methods; retrieving CLOB  I am trying to insert a string into CLOB field using insert into K_OF01.K_OFT_PRC_SUBPRC_MSG (MSG_TXT) values ( 'testtest') ) there are no binds in there at all. You are not binding. what PROGRAMMING language are you using, tibco is not a language.

New Java: Java 1.4, Java 5, and Java 6, To set the rest of the query one uses setString, setInt, setFloat, setObject, or other Clob CLOB long/Long BIGINT java.sql. not work with a TIMESTAMP database type when using Oracle and setString may not be for conversion discussion. * This program demonstrates a technique for converting a large String * to an oracle.sql.CLOB then inserting that CLOB into a database. * I believe most of this is specific to Oracle, specifically the * ability to create a temporary CLOB within your program. * -----*/ class TestOraCLobInsert {public static void main (String args []) throws

Comments
  • You can simply use setString() on the PreparedStatement. No need to create the intermediate Clob instance.
  • @BugalugsNash: You incorrectly assume that Clob.toString() returns the Clob contents. That's not the case. Your debug output therefore doesn't help diagnosing the problem. And I'm not sure you have a problem at all.
  • @a_horse_with_no_name I'm trying to insert a row into a table which has Clob as the data type, so I'm using a prepared statement with a variable '?', and then trying to bind a Clob to that variable. I don't understand what you're suggesting, but I assume it doesn't apply to me.
  • @Codo how do I access the clob contents? the toString() method is what google suggests, and it returns a string so I don't see the problem
  • As I said: there is no need to use the Clob instance. setString() directly on the statement will work just fine. See my answer.