14

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.

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 1
    Why "can't [you] have ConnectionManager"? Could you use [`LobCreator`](http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/engine/jdbc/LobCreator.html)? – radimpe Jul 11 '12 at 06:56

7 Answers7

25

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());
  • Exactly what i am looking for ;-) – jumping_monkey Oct 21 '19 at 10:00
  • In my case I've returned `clob` from a *java stored function* but got this error - *ORA-00932: inconsistent datatypes: expected a return value that is an instance of a user defined Java class convertible to an Oracle type got an object that could not be converted* – Erfan Ahmed Jan 06 '20 at 04:16
  • 1
    Hibernate has a utlity too in case you want to use for CLOB and BLOB. **Hibernate.createClob(yourJavaString)** For BLOB **Hibernate.createBlob(yourJavaString.getBytes())** – Reddymails Mar 01 '22 at 12:40
10

Throws warning: Clob not initialized.

You need an OracleConnection to create a Clob, using the Oracle Database.

OracleConnection conn;  // initialize this first

Clob myClob = conn.createClob();



private OracleConnection conn = null;
public void setConnection( OracleConnection conn )
{
    this.conn = conn;
}

void setClob( String cookie ) throws SQLException
{
    Clob myClob = conn.createClob();
    myClob.setString( 1, cookie);
}
Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
Mike
  • 101
  • 1
  • 3
8

Try this :

OracleConnection conn;  // initialize this first

CLOB clob = conn.createClob();

public Clob getClob(String data){

    return clob.setString(position, data);
}
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
vikiiii
  • 9,246
  • 9
  • 49
  • 68
2

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;
eby
  • 111
  • 1
  • 4
1

On my side, I use

Clob generateProxy(String string)

From

import org.hibernate.engine.jdbc.ClobProxy;
Greg7000
  • 297
  • 3
  • 15
1

If you're using spring boot and you only have a JdbcTemplate obj you can get an Oracle connection using:

private JdbcTemplate  jdbcTemplate;   // or NamedParameterJdbcTemplate 
Clob myClob = this.jdbcTemplate.getJdbcTemplate().getDataSource().getConnection().createClob();
DS.
  • 604
  • 2
  • 6
  • 24
0

If you are looking for an opportunity to create a [N]Clob without a Connection you can use NonContextualLobCreator from the Hibernate project. Following example shows the create of an NCLob using a string

String xml = "my blob content";
NClob clob = NonContextualLobCreator.INSTANCE.createNClob(xml);
entity.setXmlclob);

Available from at least Hibernate 4.2 (maybe earlier).

bish
  • 3,381
  • 9
  • 48
  • 69