问题描述
我用的是java 后台访问oracle给oracle中的nclob类型的字段插入string类型的对象logresult,如果对象的长度过长时提示ORA-01460: unimplemented or unreasonable conversion requested在网上查说jar包用class12不行,我就改成ojdbc14.jar,但是问题依旧,打印报错时string对象logresult长度为3277.如果长度短一切正常。在网上找了一种用io来处理这个string对象logresult的方法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();这个时候操作成功!但是,一旦string类型长度过长,则乱码:字符串短则正常:这个到底是怎么回事,字符串长了为什么会全部乱码啊?帮忙解答下啊,我弄了好几天都没弄出来。。。 问题补充:Frankie199 写道
解决方案
研究了一中午,终于把这个问题解决了!1.创建test数据表(nclob好像不行,我改为clob了,不知是否影响你的使用?):create table nclob_test (id number primary key, content clob);2.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"); // 示例文件大小为200kInputStream 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方式selectPreparedStatement smt = con.prepareStatement("select id, content from nclob_test where id=1 for update");// 更新clob值 注意必须将commit方式置为falseboolean 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);}}
解决方案二:
这里有个帖子http://kb.cnblogs.com/a/890860/他是C#,希望对你有帮助,Good Luck