我用的是java 后台访问oracle
给oracle中的nclob类型的字段插入string类型的对象logresult,如果对象的长度过长时提示
ORA-01460: unimplemented or unreasonable conversion requested
在网上查说jar包用class12不行,我就改成ojdbc14.jar,但是问题依旧,打印报错时string对象logresult长度为3277.
如果长度短一切正常。
在网上找了一种用io来处理这个string对象logresult的方法
[code="java"]
byte[] bytes_logresult =logresult.getBytes();
ByteArrayInputStream result = new ByteArrayInputStream( bytes_logresult );
InputStreamReader clobReader = new InputStreamReader(result);
stmt.setCharacterStream(1,clobReader ,bytes_logresult.length);
stmt.executeUpdate();
[/code]
这个时候操作成功!但是,一旦string类型长度过长,则乱码:
[img]http://dl.iteye.com/upload/attachment/287503/b8f56f7c-ee88-327f-93be-69a1257fa23a.jpg[/img]字符串短则正常:
[img]http://dl.iteye.com/upload/attachment/287505/c9c199c4-35d7-366c-a55c-e564866e90f7.jpg[/img]
这个到底是怎么回事,字符串长了为什么会全部乱码啊?帮忙解答下啊,我弄了好几天都没弄出来。。。
研究了一中午,终于把这个问题解决了!
1.创建test数据表(nclob好像不行,我改为clob了,不知是否影响你的使用?):
[code="java"]create table nclob_test (id number primary key, content clob);[/code]
2.java代码:
[code="java"]
public class TestClob{
public static void write_nclob(Connection con) throws SQLException,
IOException {
// 1.新增数据时clob字段值为empty_clob()
PreparedStatement smt = con
.prepareStatement("insert into nclob_test(id, content) values(?, empty_clob())");
smt.setInt(1, 1);
smt.executeUpdate();
smt.close();
//此处演示从文件中加载内容
File f = new File("e:\\促进企业节能增效.txt"); // 示例文件大小为200k
InputStream is = new FileInputStream(f);
byte[] data = new byte[(int) is.available()];
is.read(data);
is.close();
String buf = new String(data);
//写clob数据到数据库
update_nclob(con, buf);
}
public static void update_nclob(Connection con, String value) throws SQLException,
IOException {
// 此处注意用for update方式select
PreparedStatement smt = con
.prepareStatement("select id, content from nclob_test where id=1 for update");
// 更新clob值 注意必须将commit方式置为false
boolean old_c = con.getAutoCommit();
con.setAutoCommit(false);
ResultSet rs = smt.executeQuery();
if (rs.next()) {
Clob clob = rs.getClob(2);
BufferedWriter out = new BufferedWriter(((oracle.sql.CLOB) clob)
.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new StringReader(value));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
out.close();
con.commit();
smt.close();
}
con.setAutoCommit(old_c);
}
public static void load_nclob(Connection con) throws SQLException,
IOException {
PreparedStatement smt = con
.prepareStatement("select * from nclob_test");
ResultSet rs = smt.executeQuery();
if (rs.next()) {
Clob c = rs.getClob(2);
char[] cbuf = new char[4096];
c.getCharacterStream().read(cbuf);
String content = c.getSubString(1, (int) c.length());
// System.out.println(content);
System.out.println(new String(cbuf));
}
rs.close();
smt.close();
}
public static void main(String[] args) throws SQLException, IOException {
Connection con = 此处省略与数据库的连接,这个大家都会吧?!
write_nclob(con);
load_nclob(con);
}
}
[/code]
这里有个帖子
http://kb.cnblogs.com/a/890860/
他是C#,希望对你有帮助,Good Luck