Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to CLOB conversion

 
shachindra singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body have the sample code for String to CLOB conversation.

 
shachindra singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A casual glance at the JavaDocs for java.sql.Clob interface finds the setString method.

Have you tried that?

Bill
 
shachindra singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the above thing solved

JAR: OJDBC6
JDK: 1.6

Code:



Thank you Code Ranch
 
maria andreson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Driver is working fine is updating data for 30 Thousand characters.

I am now implementing the above like this

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic