Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert CLOB using JDBC

 
Liwuen Chew
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I have a question regarding inserting clobs in oracle. I have a table

table: field1 - string ; field2 - number; field 3 - clob.

And I want to insert the values like this:
"insert into table values('abc',123,<don't know what to do here> )".

I am using a JDBC connection. Already tried with prepareStatement and things like that, but I cannot make it to work. Can someone explain how to make this kind of insert from java?

Thank you!
 
Liwuen Chew
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! I found the way.
You must use an OraclePreparedStatement . It must be something like this:

String sql = "insert into table values('abc',123,?)";
OraclePreparedStatement st = (OraclePreparedStatement) connection.prepareStatement(sql); //where connection is your connection with Oracle
st.setStringForClob(1, /*String variable which contains the CLOB string*/);
st.executeUpdate();

And that's it!. Hope this helps anyone with the same issue!
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for solving your own topic!

And a SPECIAL thanks for posting the answer here!!
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liwuen Chew wrote:Hello everyone! I found the way.
You must use an OraclePreparedStatement . It must be something like this:

String sql = "insert into table values('abc',123,?)";
OraclePreparedStatement st = (OraclePreparedStatement) connection.prepareStatement(sql); //where connection is your connection with Oracle
st.setStringForClob(1, /*String variable which contains the CLOB string*/);
st.executeUpdate();

And that's it!. Hope this helps anyone with the same issue!


It gives me the following error:

 
Farakh khan
Ranch Hand
Posts: 833
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import oracle.jdbc.OraclePreparedStatement;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic