将Java String转换为CLOB

时间:2011-03-28 04:52:57

标签: java oracle jdbc clob

我有下表: enter image description here

问题是,当我尝试使用JDBC将String插入此字段时,我总是会收到以下错误:

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

我现在该怎么办?

生成INSERT的java代码如下:

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.

不确定CREATE TABLE是怎样的,正如有人为我做的那样

使用上面的当前代码,我现在得到了这个:

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)

在线:

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

没有任何意义,因为它可能是空的

1 个答案:

答案 0 :(得分:5)

您似乎正在使用setString而不是setCharacterStream来设置clob的值。如果您使用setString,则驱动程序可能会将值转换为varchar2,其大小限制为4000个字符。

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);
   }
}

相关文件: