Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Clob in Java

I have a situation where I need to make Clob object from String. The problem is I can't have ConnectionManager in that method.

I need to some utility like

 public Clob getClob(String data){

 }

Can any one tell me how can I make this.

I have oralce.sql.CLOB also. however it requires Connection to create object.

like image 284
RaceBase Avatar asked Jul 11 '12 06:07

RaceBase


People also ask

Can we convert CLOB to string?

To convert CLOB data type to string Reader r = clob. getCharacterStream(); Read each character one by one from the retrieved Stream of characters and append them to the StringBuilder or StringBuffer.

How do I cast an object to CLOB?

The code you submitted in your question should become: String sql = "select id, data from mytable"; List< Object[] > results = getEntityManager(). createNativeQuery(sql). getResultList(); Map< Long, String > map = new HashMap<>(); Clob clob = (Clob)result[1]; String value = clob.

What is CLOB format?

A CLOB (character large object) value can be up to 2,147,483,647 characters long. A CLOB is used to store unicode character-based data, such as large documents in any character set.


2 Answers

Those who are still looking for an alternate answer, a Clob object could be created without the need for a connection object as shown below.

Clob myClob = new javax.sql.rowset.serial.SerialClob(stringData.toCharArray());
like image 179
Nibin Jacob Panicker Avatar answered Sep 18 '22 05:09

Nibin Jacob Panicker


In order to initialize the OracleConnection mentioned in the other answers here and if you're doing this for a java stored procedure and the connection is to the database where the procedure is stored, then the Connection can be initialized like this:

Connection conn = DriverManager.getConnection("jdbc:default:connection:");

and this import is needed:

import oracle.jdbc.driver.OracleConnection;
like image 42
eby Avatar answered Sep 18 '22 05:09

eby