RaceBase
RaceBase

Reputation: 18848

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.

Upvotes: 14

Views: 113796

Answers (7)

DS.
DS.

Reputation: 612

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();

Upvotes: 1

Greg7000
Greg7000

Reputation: 425

On my side, I use

Clob generateProxy(String string)

From

import org.hibernate.engine.jdbc.ClobProxy;

Upvotes: 1

bish
bish

Reputation: 3419

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).

Upvotes: 0

eby
eby

Reputation: 111

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;

Upvotes: 2

vikiiii
vikiiii

Reputation: 9456

Try this :

OracleConnection conn;  // initialize this first

CLOB clob = conn.createClob();

public Clob getClob(String data){

    return clob.setString(position, data);
}

Upvotes: 8

Nibin Jacob Panicker
Nibin Jacob Panicker

Reputation: 331

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());

Upvotes: 29

Mike
Mike

Reputation: 111

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);
}

Upvotes: 11

Related Questions