0

I have the following table: enter image description here

The problem is that when I try to use JDBC to insert a String to this field I always get the following error:

java.sql.SQLException: Data size bigger than max size for this type: 6019

what should I do now?

The java code to generate the INSERT is the following:

Connection conn2 = null;
   if(connection==null||connection.isClosed())
   {
    System.out.println("Connection is null");
    conn2 = connectDB();
   }
   //REPLACE is non-standard SQL from MySQL, it's counter part of INSERT IGNORE
   String sql = ""; 
   sql = "INSERT INTO TEST."+tablename+" ("+fields+") VALUES ("+fields.replaceAll("[^,]+", "?")+")";

   System.out.println("SQL: "+sql);
   PreparedStatement pstmt =  conn2.prepareStatement(sql);

   // start parsing
   int event = r.getEventType();
   while (true) {
    // for each wanted element 
    if (event == XMLStreamConstants.START_ELEMENT
      && r.getName().toString().equalsIgnoreCase("row")) {
     System.out.println("Table : "+tablename+" / Row : "+rowCount );
     if (attributeCount == 0)
      attributeCount = r.getAttributeCount();


     //put each parameter to SQL
     int f=1;
     for (String field : fieldsArray){
    String value = r.getAttributeValue("",field);
    if("body".equalsIgnoreCase(field) && value != null) {
         pstmt.setCharacterStream(f++, new CharArrayReader(value.toCharArray()), value.length());
    } else {
         pstmt.setString(f++, value);
    }
 }

     pstmt.addBatch();
     rowCount++;

     if(rowCount%rowspercommit==0){
      System.out.println("Importing at row "+rowCount+" ... ");
      pstmt.executeBatch();
      conn2.commit();
     }
    } // end for each row.

Not sure how the CREATE TABLE is, as someone did that for me

With the current code above, I am now getting this:

Exception in thread "main" java.lang.NullPointerException
    at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
    at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
    at oracle.jdbc.driver.OraclePreparedStatement.checkBindTypes(OraclePreparedStatement.java:3271)
    at oracle.jdbc.driver.OraclePreparedStatement.setStreamItem(OraclePreparedStatement.java:1178)
    at oracle.jdbc.driver.OraclePreparedStatement.setCharacterStream(OraclePreparedStatement.java:3539)
    at XMLDumpImporter.importXMLFile(XMLDumpImporter.java:179)
    at XMLDumpImporter.importXMLFolder(XMLDumpImporter.java:117)
    at XMLDumpImporter.main(XMLDumpImporter.java:48)

at line:

if("body".equalsIgnoreCase(field) && value != null) {
             pstmt.setCharacterStream(f++, new CharArrayReader(value.toCharArray()), value.length());

Which made no total sense as what could possibly be null

2
  • Can you show the insert statement and perhaps the create table statement? And the Java code you use to set the value of the clob? Commented Mar 28, 2011 at 4:57
  • edited my question above Commented Mar 28, 2011 at 5:10

1 Answer 1

6

It seems that you are using setString instead of setCharacterStream to set value of the clob. If you are using setString then the driver probably converts the value to varchar2 which has size limit of 4000 characters.

Hackety hack:

int fieldIndex = 0;
for(String fieldName : fieldsArray) {
   String value = r.getAttributeValue("",field);
   if(value != null && "body".equalsIgnoreCase(fieldName)) {
     pstmt.setCharacterStream(++fieldIndex, new StringReader(value), value.length());
   } else {
     pstmt.setString(++fieldIndex, value);
   }
}

Relevant documentation:

Sign up to request clarification or add additional context in comments.

13 Comments

well could you use setCharacterStream when you have a String? I modified my code above, how should I use setCharacterStream?
@EquinoX - if you have the content in a String already you can wrap the String into a CharArrayReader or a ByteArrayInputStream
sometimes it gives me the error Exception in thread "main" java.lang.NullPointerException: if("body".equalsIgnoreCase(fieldName)) { pstmt.setCharacterStream(fieldName, new CharArrayReader(value.toCharArray()), value.length()); }
check for nullity of the value and proceed as fit
I actually did check if value == null and it never hit there... weird.. I can't figure out where this null is from
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.