wood burning stoves*
The moose likes JDBC and the fly likes String to CLOB conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "String to CLOB conversion" Watch "String to CLOB conversion" New topic
Author

String to CLOB conversion

shachindra singh
Greenhorn

Joined: Feb 08, 2011
Posts: 5
Can any body have the sample code for String to CLOB conversation.

shachindra singh
Greenhorn

Joined: Feb 08, 2011
Posts: 5
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
A casual glance at the JavaDocs for java.sql.Clob interface finds the setString method.

Have you tried that?

Bill
shachindra singh
Greenhorn

Joined: Feb 08, 2011
Posts: 5
William Brogden wrote:A casual glance at the JavaDocs for java.sql.Clob interface finds the setString method.

Have you tried that?

Bill

Thanks for reply.
I am tring store xml in database. XML is in StringBuffer and i need to convert that in clob.
i have tried this code. runtime error occure
Exception in thread main
java.lang.AbstractMethodError: int java.sql.Clob.setString(long, java.lang.String)

at com.nucleus.los.util.test2.main(test2.java:61)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Apparently the SQL library you are trying to use does not support that method in the Clob interface.

It really should have thrown a SQLFeatureNotSupportedException according to the JavaDocs but in any case you are going to have to hunt for another approach.

There are other methods in Clob, how about the setCharacterStream() call to get a Writer you can write the String to?

Maybe you can find some documentation on your SQL library as to which methods it supports.

Bill

shachindra singh
Greenhorn

Joined: Feb 08, 2011
Posts: 5
William Brogden wrote:Apparently the SQL library you are trying to use does not support that method in the Clob interface.

It really should have thrown a SQLFeatureNotSupportedException according to the JavaDocs but in any case you are going to have to hunt for another approach.

There are other methods in Clob, how about the setCharacterStream() call to get a Writer you can write the String to?

Maybe you can find some documentation on your SQL library as to which methods it supports.

Bill



Thanks for the reply
i have also tried this
Clob c13 =null;
BufferedWriter br = new BufferedWriter(c13.setCharacterStream(0));
StringReader strrd=new StringReader(XML.toString());
char aux;
do{
aux=(char)strrd.read();
br.write(aux);
System.out.println("the value is"+aux);
}while(aux != -1);

in this i get the null pointer error. in
BufferedWriter br = new BufferedWriter(c13.setCharacterStream(0));
this line .
Please suggest .the way to resolve the issue.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Well of course it is null, you just set the reference to null



You better look into the usual methods for getting a reference to a CLOB - can't help you there, I have not used JDBC in years.

Bill
Neelam Hankare
Greenhorn

Joined: Jun 12, 2011
Posts: 3
Hi shachindra,

Do you got any solution for this. I am also looking for same thing..I want to save XML file data into database using store procedure.
IN parameter of this store procedure is CLOB and have to convert this xml data into java.sql.Clob before storing.
Please provide some code snippet if you got the solution for this.

Thanks in advance.
Niiraj Patel
Ranch Hand

Joined: Sep 17, 2011
Posts: 47
Hi,

Typically, the Clob class will be provided by the JDBC driver vendor. So converting a string into Clob depends on the API provided by the vendor.
For Oracle it is present in the Oracle.jar. Full classname is oracle.sql.CLOB

You create an instance of CLOB by
DBConnection connection = //get connection;
String s = //3000 chars;
oracle.sql.CLOB cl = new oracle.sql.CLOB(connection);
cl.putString(0, s);

Alternativley you could also write:
oracle.sql.CLOB cl = new oracle.sql.CLOB(connection, s.getBytes());

You retrieve the string by:
String s = cl.getSubString(0, cl.length());

Thanks,
Niraj
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Hello To all

I am searching on the said topic. I found various forums discussing on this topic, but i am not getting anything out of it. The sample code mentioned around does not work for me.

Kindly check how i write sample code for OJDBC14 jar



and i get following error



If anyone can provide me any working sample code then i shall be thankful to him :P


Regards
Azrael Noor
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Got the above thing solved

JAR: OJDBC6
JDK: 1.6

Code:



Thank you Code Ranch
maria andreson
Greenhorn

Joined: Apr 17, 2012
Posts: 1
It seems that you are using setString instead of setCharacterStream to set value of the clob. If you are using setString then the driver probably converts the value to varchar2 which has size limit of 4000 characters..


Pizza Express Vouchers
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
The Driver is working fine is updating data for 30 Thousand characters.

I am now implementing the above like this

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to CLOB conversion
 
Similar Threads
conversion between CLOB/BLOB and String
Convert large string to Clob
Converting a String to Clob for insert into DB
saving Very Very long string to Oracle DB
Compare long varchar