incidentjava.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB

Spring JIRA | Oleksandr Gavenko | 2 years ago
  1. 0

    The only way to avoid LobHandler in DAO code for insert/update: {code:java|borderStyle=solid} MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("clob", "a long long text", Types.CLOB); namedTemplate.update(INSERT_STMT, paramSource); {code} See: * http://stackoverflow.com/questions/5791662/inserting-clob-with-namedparameterjdbctemplate * http://codeblow.com/questions/placing-clob-with-namedparameterjdbctemplate/ another way (only for JdbcTemplate, not for NamedParameterJdbcTemplate): * http://docs.spring.io/spring/docs/3.0.x/reference/jdbc.html#jdbc-lob * http://www.java2s.com/Code/Java/Spring/InsertClobData.htm java.sql.Types has NCLOB constans for Java 6/7/8: * http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html#NCLOB * http://docs.oracle.com/javase/7/docs/api/java/sql/Types.html#NCLOB If I do: {{paramSource.addValue("clob", "a long long text", Types.CLOB);}} all work fine, but with: {{paramSource.addValue("clob", "a long long text", Types.NCLOB);}} I get: {code:java|borderStyle=solid} => Fail to add incidentjava.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8762) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8286) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8868) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:240) ~[ojdbc6.jar:11.2.0.3.0] at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:406) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:234) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:149) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:298) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:251) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:583) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:840) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:281) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] {code} Actual type of table column is NCLOB, and with Type.CLOB code work fine. My suggestion just add NCLOB to {{src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java}} with same role as CLOB!

    Spring JIRA | 2 years ago | Oleksandr Gavenko
    incidentjava.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB
  2. 0

    The only way to avoid LobHandler in DAO code for insert/update: {code:java|borderStyle=solid} MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("clob", "a long long text", Types.CLOB); namedTemplate.update(INSERT_STMT, paramSource); {code} See: * http://stackoverflow.com/questions/5791662/inserting-clob-with-namedparameterjdbctemplate * http://codeblow.com/questions/placing-clob-with-namedparameterjdbctemplate/ another way (only for JdbcTemplate, not for NamedParameterJdbcTemplate): * http://docs.spring.io/spring/docs/3.0.x/reference/jdbc.html#jdbc-lob * http://www.java2s.com/Code/Java/Spring/InsertClobData.htm java.sql.Types has NCLOB constans for Java 6/7/8: * http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html#NCLOB * http://docs.oracle.com/javase/7/docs/api/java/sql/Types.html#NCLOB If I do: {{paramSource.addValue("clob", "a long long text", Types.CLOB);}} all work fine, but with: {{paramSource.addValue("clob", "a long long text", Types.NCLOB);}} I get: {code:java|borderStyle=solid} => Fail to add incidentjava.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8762) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8286) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8868) ~[ojdbc6.jar:11.2.0.3.0] at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:240) ~[ojdbc6.jar:11.2.0.3.0] at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:406) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:234) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:149) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:298) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:251) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:583) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:840) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:281) ~[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE] {code} Actual type of table column is NCLOB, and with Type.CLOB code work fine. My suggestion just add NCLOB to {{src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java}} with same role as CLOB!

    Spring JIRA | 2 years ago | Oleksandr Gavenko
    incidentjava.lang.ClassCastException: java.lang.String cannot be cast to oracle.sql.CLOB

    Root Cause Analysis

    1. incidentjava.lang.ClassCastException

      java.lang.String cannot be cast to oracle.sql.CLOB

      at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical()
    2. Oracle jdbc
      OraclePreparedStatementWrapper.setObject
      1. oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8762)[ojdbc6.jar:11.2.0.3.0]
      2. oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8286)[ojdbc6.jar:11.2.0.3.0]
      3. oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:8868)[ojdbc6.jar:11.2.0.3.0]
      4. oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:240)[ojdbc6.jar:11.2.0.3.0]
      4 frames
    3. Spring Framework
      NamedParameterJdbcTemplate.update
      1. org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:406)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      2. org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:234)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      3. org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:149)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      4. org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:298)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      5. org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:251)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      6. org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:583)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      7. org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      8. org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:840)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      9. org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:281)[spring-jdbc-3.2.8.RELEASE.jar:3.2.8.RELEASE]
      9 frames