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++) { ░
obj = inArgs[i];
if( obj == null || obj instanceof String ) { ░
stmt.setString(i+1, (String)obj); ░
} else if( obj instanceof Integer ) { ░
stmt.setInt(i+1, ((Integer)obj).intValue()); ░
} else if( obj instanceof ByteArrayOutputStream ) { ░
stmt.setBytes(i+1, ((ByteArrayOutputStream)obj).toByteArray()); ░
} else { ░
throw new SQLException( "internal error: unsupported datatype
} ░
} ░
} ¨
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.