简体   繁体   中英

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.

那些仍在寻找替代答案的人,可以在不需要连接对象的情况下创建 Clob 对象,如下所示。

Clob myClob = new javax.sql.rowset.serial.SerialClob(stringData.toCharArray());

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

Try this :

OracleConnection conn;  // initialize this first

CLOB clob = conn.createClob();

public Clob getClob(String data){

    return clob.setString(position, data);
}

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;

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

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

On my side, I use

Clob generateProxy(String string)

From

import org.hibernate.engine.jdbc.ClobProxy;

I had a similar problem like you where I needed to store a JSON in a field. You dont have to use BLOB or CLOB as a java object, but you can store it as one.

to wrap my answer up, if you are using the ORACLE database(which is a database that always causes problems of speaking its language) use bellow format as a guide or best practice, which is based on oracle documentation itself, to solve your problem:

@Lob @Basic(fetch=LAZY)
@Column(name="REPORT")
protected String report;

As you see, the variable you use to wrangle data is in String(no convertion needed) but it will be stored as a LOB.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM