Skip to Main Content
  • Questions
  • insert clob in a preparestatement in java

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Chitao.

Asked: August 18, 2000 - 7:12 pm UTC

Last updated: March 08, 2005 - 7:27 am UTC

Version: 8.1.6

Viewed 1000+ times

You Asked

in java, I use prepared statement to do the insertion.
I use instanceof to judge the type of parameters. (see following code)

but how can I set the type for clob type? for long type, it is ok to set it as string. but for clob, there is a maximum limit on the size, I have a clob colum is over 10k which can not be inserted into my table.

Do you have any suggestion?


if( inArgs != null ) {
Object obj; for (int i = 0; i < inArgs.length; i++) { &#9617;
obj = inArgs[i];
if( obj == null || obj instanceof String ) { &#9617;
stmt.setString(i+1, (String)obj); &#9617;
} else if( obj instanceof Integer ) { &#9617;
stmt.setInt(i+1, ((Integer)obj).intValue()); &#9617;
} else if( obj instanceof ByteArrayOutputStream ) { &#9617;
stmt.setBytes(i+1, ((ByteArrayOutputStream)obj).toByteArray()); &#9617;
} else { &#9617;
throw new SQLException( "internal error: unsupported datatype
} &#9617;
} &#9617;
} ¨€



and Tom said...



Blobs and Clobs must be treated "special" in all Jdbc implementations. They are done via streams -- not via stmt.set* calls (although in jdbc 2 you have a setClob and setBlob -- they are much the same -- see the last referenced link below pointint to technet. The mechanics are nearly identical -- insert empty_clob(), retrieve it, and then stream it).

Here is an example -- it inserts an empty_clob() and then gets it back out -- you have to select OUT (or use the insert into ... returning clob_column into :bind_variable) lob locator and cast as a stream. This shows how to read and write clobs. There :

import java.lang.*;
import java.sql.*;
import java.util.*;
import oracle.jdbc.driver.*;


public class movelr
{
public static void main(String[] args) throws Exception
{
int length;

DriverManager.registerDriver (new OracleDriver());

Connection conn73 = DriverManager.getConnection
("jdbc:oracle:oci8:@ora8idev","scott","tiger");
Connection conn81 = DriverManager.getConnection
("jdbc:oracle:oci8:@ora8idev","scott","tiger");

conn73.setAutoCommit (false);
conn81.setAutoCommit (false);
System.out.println( "connected..." );

// Create statements
PreparedStatement pstmt = conn73.prepareStatement
("select image from image@slack" );

System.out.println( "prepared statement for v7.3..." );

PreparedStatement insertStmt = conn81.prepareStatement
("insert into blob_demo ( x, y ) " +
"values ( ?, empty_blob() )");
System.out.println
( "prepared statement for v8.x BLOB insert..." );

PreparedStatement getStmt = conn81.prepareStatement
("select y from blob_demo where x = ? for update");
System.out.println
( "prepared statement for v8.x BLOB select..." );

ResultSet rset73 = pstmt.executeQuery();
System.out.println
( "executed statement for v7.3 select..." );

for( int i = 0; rset73.next(); i++ )
{
System.out.println( "looping..." );
InputStream is = rset73.getBinaryStream(1);
System.out.println( "Got v7.3 LONG RAW stream..." );

// Insert a new row and get select
// back the BLOB column
insertStmt.setInt(1, i);
insertStmt.executeUpdate();
getStmt.setInt(1, i);
OracleResultSet rset81 =
(OracleResultSet) getStmt.executeQuery();

rset81.next();
OutputStream os =
rset81.getBLOB(1).getBinaryOutputStream();
System.out.println( "Got v8.x BLOB stream..." );

System.out.println( "Moving data from IS to OS..." );
byte buffer[] = new byte[1024];
while ((length = is.read (buffer,0,1024)) != -1)
{
os.write( buffer, 0, length );
}

is.close();
os.close();
rset81.close();
}
rset73.close();
insertStmt.close();
getStmt.close();
pstmt.close();
conn81.commit();
conn73.close();
conn81.close();
}
}


See also:

</code> http://docs.oracle.com/cd/A81042_01/DOC/java.816/a81354/oralob2.htm#1043351 <code>

for more details.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

What if you have a String to write in

Tom Henricksen, February 15, 2005 - 4:12 pm UTC

I have a java string that I need to put in to the clob column.

connection = QNetDatabaseConnection.getUserAdminDataSource();
insertStmt = connection.prepareStatement(INSERT_MESSAGE);
getStmt = connection.prepareStatement(SELECT_MESSAGE);

oracle.sql.DATE oraceldate = new oracle.sql.DATE(date);
insertStmt.setObject(1, oraceldate);
insertStmt.setString(2, ticket);
insertStmt.setString(3, senderId);
insertStmt.setString(4, appKey);
insertStmt.executeUpdate();
getStmt.setString(1,ticket);
OracleResultSet rs = (OracleResultSet) getStmt.executeQuery();

rs.next();
OutputStream os = rs.getBLOB(1).getBinaryOutputStream();

System.out.println("Moving data from IS to OS...");
byte buffer[] = message.toString().getBytes();
while ((length = is.read(buffer, 0, 1024)) != -1) {
os.write(buffer, 0, length);
}

problem with multiple inserting of LOBs/LONGs

A reader, March 07, 2005 - 3:10 pm UTC

Hi,

in the documentation of 8.1.7.6 or higher i found that the JDBC's thick driver setBinaryStream/setCharacterStream methods are supported to set LOB values

i want to use the same prepared statement in order to insert several rows with the same value.
to do that i set the value in the statement once and call the
executeUpdate several times.

i tried to insert into a table that was created using:

CREATE TABLE test (
l CLOB
);

with a code similar to this:

PreparedStatement stmt = conn.prepareStatement("INSERT INTO test VALUES(?)");

String s = getSomeRandomString(); // Return a string whose length is over 4000

CharArrayReader r = new CharArrayReader(s.toCharArray());

stmt.setCharacterStream(1, r, s.length());

stmt.executeUpdate();
stmt.executeUpdate();


what happens is that the first row is inserted as i intended.
the second (or more) is inserted with NULL.
(i can't call the reset method of the stream after the execute cause the setCharacterStream/setBinaryStream closes the stream).

ALSO happens with LONG / LONG RAW columns.

This doesn't happen with values smaller then 4000.


i also tested it with 9.2.0.6 and it doesn't work.


i'm doing something that shouldn't work ?
seams quiet simple


some help will be great .. :)
thanx.


Tom Kyte
March 08, 2005 - 7:27 am UTC

I would use metalink.oracle.com for this one.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here