简体   繁体   English

如何使用Java在Oracle中插入

[英]How to Insert in Oracle using java

I am having CLOB column in Oracle Data Base , I want to insert String . 我在Oracle数据库中有CLOB列,我想插入String。

It works if I use setCharacterStream, but how to insert String by setBytes am getting exception. 如果我使用setCharacterStream,它会起作用,但是如何通过setBytes插入String会出现异常。 Please help me. 请帮我。

String s = "Hello How are you Data for CLOB column";
ps.setCharacterStream(1, new StringReader(s), s.length());
ps.setByte(1,Byte.parseByte(s));

Exception Trace : 异常跟踪:

java.lang.NumberFormatException: For input string: "Hello How are you Data for CLOB column"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Byte.parseByte(Byte.java:151)
    at java.lang.Byte.parseByte(Byte.java:108)
    at colb.test.InertClob.main(InertClob.java:24)

Here are two good examples (with sample code, for select and insert): 这是两个很好的示例(带有示例代码,用于选择和插入):

  • Handling CLOBS made easy: 轻松处理CLOBS:

http://rocksolutions.wordpress.com/2010/06/07/handling-clobs-made-easy-with-oracle-jdbc-10g/ http://rocksolutions.wordpress.com/2010/06/07/handling-clobs-made-easy-with-oracle-jdbc-10g/

  • Adding large object type to databaase 将大型对象类型添加到数据库

http://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html http://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html

please refer to the Java API DOC 请参考Java API DOC

Parses the string argument as a signed decimal byte. 将字符串参数解析为带符号的十进制字节。 The characters in the string must all be decimal digits, 字符串中的字符必须全部为十进制数字,

You should: 你应该:

  1. Turn the String to a byte array by calling s.getBytes() for example or any other. 例如,通过调用s.getBytes()或任何其他方法,将字符串转换为字节数组。
  2. Call setBytes method, not setByte 调用setBytes方法,而不是setByte

  3. When retrieving from the database, don't forget how you got the bytes, in order to restore the String properly. 从数据库检索时,请不要忘记如何获取字节,以便正确还原String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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