4

I’m not sure how to use Java/JDBC to insert a very long string into an Oracle database.

I have a String which is greater than 4000 characters, lets say it’s 6000. I want to take this string and store it in an Oracle database.

The way to do this seems to be with the CLOB datatype. Okay, so I declared the column as description CLOB.

Now, when it comes time to actually insert the data, I have a prepared statement pstmt. It looks like pstmt = conn.prepareStatement(“INSERT INTO Table VALUES(?)”).

So I want to use the method pstmt.setClob(). However, I don’t know how to create a Clob object with my String in it; there's no constructor (presumably because it can be potentially much larger than available memory).

How do I put my String into a Clob?

Keep in mind I’m not a very experienced programmer; please try to keep the explanations as simple as possible. Efficiency, good practices, etc. are not a concern here, I just want the absolute easiest solution. I’d like to avoid downloading other packages if it all possible; right now I’m just using JDK 1.4 and what is labelled ojdbc14.jar. I've looked around a bit but I haven't been able to follow any of the explanations I've found.

If you have a solution that doesn’t use Clobs, I’d be open to that as well, but it has to be one column.

4 Answers 4

8

You have (at least) two options:

  • use connection.createClob() to create a Clob, set the data on it, and set it on the prepared statement. This will work for smaller data

  • use preparedStatement.setClob(position, reader) - here you will have a Reader instance.

4
  • Ah, createClob() is exactly what I was looking for. Thanks. Actually, I'm getting the error: Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.OracleConnection.createClob()Ljava/sql/Clob; when I run it. It comes from the line: Clob description = conn.createClob();
    – Ventrue
    May 22, 2010 at 8:09
  • 2
    that might mean your jdbc driver isn't up to date.
    – Bozho
    May 22, 2010 at 8:12
  • Well, the database is 10.1.0.3, I don't see any JDBC driver downloads for that version on Oracle's website...I'm presuming that what I have is 10.1.0.3, but I can't be certain. It might be worth noting that I'm using Class.forName("oracle.jdbc.driver.OracleDriver"); rather than an import statement. I don't really know much about this, but it seems like it could be relevant.
    – Ventrue
    May 22, 2010 at 8:51
  • 4
    Okay, I got the 10.1.0.5 drivers. I decided to use the Property class and set "SetBigStringTryClob" to "true", so that I can just use pstmt.setString and it won't complain if the string is long.
    – Ventrue
    May 22, 2010 at 9:12
3

Also, this below solution explains how to handle this using PL/SQL

Reference: http://gogates.blogspot.com/2013/09/inserting-clob-data-which-is-more-than.html


Inserting CLOB data which is more than 4000 characters into Oracle Table I was trying to insert 4000+ characters data into an Oracle table having CLOB as data type, but could not do that.. using simple insert statement

Table Structure which I used is as below

CREATE TABLE ClobTest (id number, clobData clob );

Table structure is pretty simple, having only 2 columns a number and a clob data type.

Now... if you run below query it will get execute without any error

INSERT INTO ClobTest VALUES(1, 'ClobTestData');

However, when you increase the length of your data set 4000+ it will not succeed.and will give error as below

Error report: SQL Error: ORA-01704: string literal too long 01704. 00000 - "string literal too long" *Cause: The string literal is longer than 4000 characters. *Action: Use a string literal of at most 4000 characters. Longer values may only be entered using bind variables.

After googling this issue, came to know that, if you want to insert 4000+ char data into CLOB data type, try to do that using PL-SQL block.

Root cause of this is, Oracle SQL do not support char / varchar data type which is more than 4000 characters; however PL-SQL does so.

I used below anonymous pl-sql to insert 4000+ character data into CLOB data type

DECLARE

vClob VARCHAR(8000);

BEGIN

vClob := '<Charater data having 4000+ characters>';

INSERT INTO ClobTest VALUES(1, vClob);

END;

To verify if really 4000+ characters got inserted into target executed below query

SELECT LENGHT(clobData) FROM ClobTest 

I hope that helps

1

Here is an example at oracle.com for using LOB columns with Oracle and JDBC. Basically, it's inserting a LOB locator for an empty LOB (actually two, since it's demonstrating both BLOBs and CLOBs), locking the row for update, and then using the BLOB and CLOB OutputStream interfaces to write the data into the LOBs. When the "SELECT ... FOR UPDATE" is released, the LOBs will be refreshed in the database and the newly inserted data will be visible.

0

There are several methods see Java Database Connectivity (JDBC) - Convert String to CLOB and Insert into Oracle database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.