简体   繁体   中英

How to Insert in Oracle using java

I am having CLOB column in Oracle Data Base , I want to insert String .

It works if I use setCharacterStream, but how to insert String by setBytes am getting exception. 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:

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

please refer to the 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.
  2. Call setBytes method, not setByte

  3. When retrieving from the database, don't forget how you got the bytes, in order to restore the String properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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