Friday, January 22, 2010

Converting Clob Datatype to String and ViceVersa .

Facebook

If you have been using Hibernate 3.x.x or greater you can easily convert clob type to string type and vice versa, i.e String data type to Clob type.

Following piece of codes demonstrate the process of doing the conversion.


.........
.........

Clob textClobValue;
String textValue;
String newTextValue;
Clob newTextClobValue;
InputStream textStream;
Paragraph paragraph // my domain Class that contains a clob datatypes and used to update the database connection with hibernate connection (Hibernate version >=3.0)
...........
...........

textClobValue=paragraph.getText();
textStream = textClobValue.getAsciiStream(); //return inputstream
textValue=convertStreamToString(textStream);
newTextValue=textValue.replaceAll("Some text","new Text"); //Replacing some text from old text.

newTextClobValue=Hibernate.createClob(newTextValue);
//System.out.println("Updated="+newParaContentValue);
paragraph.setText(newTextClobValue);
..........
..........
..........



public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
return sb.toString();
}



0 comments:

Post a Comment

I love to entertain onymous user Comment !

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP