Java 使用 setString() 从 String 转换为 Clob 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18941928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:43:54  来源:igfitidea点击:

Converting from String to Clob using setString() not working

javajdbcderbyclob

提问by Bugalugs Nash

I am trying to convert a String into a Clob to store in a database. I have the following code:

我正在尝试将 String 转换为 Clob 以存储在数据库中。我有以下代码:

Clob clob = connection.createClob();
System.out.println("clob before setting: " + clob);
clob.setString(1,"Test string" );
System.out.println("clob after setting: " + clob);
System.out.println("clob back to string: " + clob.toString());

When I run this the Clob is not being set, the output is as follows:

当我运行这个 Clob 没有被设置,输出如下:

clob before setting: org.apache.derby.impl.jdbc.EmbedClob@1f5483e
clob after setting: org.apache.derby.impl.jdbc.EmbedClob@1f5483e
clob back to string: org.apache.derby.impl.jdbc.EmbedClob@1f5483e

Everywhere I look says to use the setString method, I have no idea why this isn't working for me.

我看到的每个地方都说要使用 setString 方法,我不知道为什么这对我不起作用。

采纳答案by a_horse_with_no_name

You don't need the intermediate Clobinstance, just use setString()on the PreparedStatement:

您不需要中间Clob实例,只需setString()PreparedStatement

PreparedStatement stmt = connection.prepareStatement("insert into clob_table (id, clob_column) values (?,?)";
stmt.setInt(1, 42);
stmt.setString(2, "This is the CLOB");
stmt.executeUpdate();
connection.commit();

回答by Vasil Lukach

Not sure if it works for derby, but for hibernate you can use:

不确定它是否适用于德比,但对于休眠,您可以使用:

public Clob createClob(org.hibernate.Session s, String text) {
    return s.getLobHelper().createClob(text);
}

回答by Al W

What you are reading from your System.out.println statements are not indicative of what is actually happening in the Clob. You are simply reading out the default java.lang.Object.toString() method of the Clob, which in this case is outputting the instance ID, and that does not change no matter what you put in it.

您从 System.out.println 语句中读取的内容并不表示 Clob 中实际发生的情况。您只是读出 Clob 的默认 java.lang.Object.toString() 方法,在这种情况下,它输出实例 ID,无论您放入什么都不会改变。

You are using the Clob properly to load the String on to it, but not read the String value back. To read the String value from a clob use...

您正在正确使用 Clob 将字符串加载到它上面,但没有读回字符串值。要从 clob 中读取 String 值,请使用...

Clob clob = connection.createClob();
clob.setString(1,"Test string" );
String clobString = clob.getSubString(1, clob.length())
System.out.println(clobString);

BTW: if you are using large strings, you DO want to convert your String to a Clob, if the String is larger than the Oracle varchar2 limit and you send a simple String it could truncate the input or blow up. If the Oracle proc calls for a Clob, give it one.

顺便说一句:如果您使用大字符串,您确实希望将您的字符串转换为 Clob,如果字符串大于 Oracle varchar2 限制并且您发送一个简单的字符串,它可能会截断输入或爆炸。如果 Oracle proc 需要一个 Clob,就给它一个。