[英]Convert String to Clob in Java
I have a situation where I need to make Clob object from String.我有一种情况需要从 String 制作 Clob 对象。 The problem is I can't have ConnectionManager in that method.问题是我不能在该方法中使用 ConnectionManager。
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.我也有oralce.sql.CLOB。 however it requires Connection to create object.但是它需要 Connection 来创建对象。
那些仍在寻找替代答案的人,可以在不需要连接对象的情况下创建 Clob 对象,如下所示。
Clob myClob = new javax.sql.rowset.serial.SerialClob(stringData.toCharArray());
Throws warning: Clob not initialized.抛出警告:Clob 未初始化。
You need an OracleConnection to create a Clob, using the Oracle Database.您需要一个 OracleConnection 来使用 Oracle 数据库创建 Clob。
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:为了初始化此处其他答案中提到的 OracleConnection,并且如果您为 java 存储过程执行此操作并且连接是到存储该过程的数据库,则可以像这样初始化 Connection:
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:如果您使用的是 spring boot 并且您只有一个 JdbcTemplate obj,则可以使用以下方法获得 Oracle 连接:
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.如果您正在寻找机会在没有连接的情况下创建[N]Clob
,您可以使用 Hibernate 项目中的NonContextualLobCreator
。 Following example shows the create of an NCLob
using a string以下示例显示了使用字符串创建NCLob
String xml = "my blob content";
NClob clob = NonContextualLobCreator.INSTANCE.createNClob(xml);
entity.setXmlclob);
Available from at least Hibernate 4.2 (maybe earlier).至少可从 Hibernate 4.2(可能更早)获得。
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.我有一个像你一样的问题,我需要在一个字段中存储一个 JSON。 You dont have to use BLOB or CLOB as a java object, but you can store it as one.您不必将 BLOB 或 CLOB 用作 java 对象,但您可以将其存储为一个对象。
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:总结我的答案,如果您使用的是ORACLE数据库(该数据库总是会导致说它的语言问题),请使用波纹管格式作为指南或最佳实践,它基于 oracle 文档本身,以解决您的问题:
@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.如您所见,用于处理数据的变量位于字符串中(无需转换),但它将存储为 LOB。
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.