Getting “Invalid column type” excecption, while using NamedParameterJDBCTemplate for insertion

后端 未结 6 1735
情深已故
情深已故 2021-01-12 17:11

I am using below code while inserting a row into database(oracle 10g xe,jar: ojdbc14.jar)

String sql = \"INSERT INTO SPONSOR_TB(ID,NAME,INDUSTRY_TYPE,IS_REPO         


        
相关标签:
6条回答
  • 2021-01-12 17:40

    for me this issue happened when i try to set null value on timestamp column. I changed the value timestamp(0). This seems to me a driver issue. I was using oracle 10g / ojdbc14.jar / oracle.jdbc.OracleDriver

       java.sql.SQLException: Invalid column type
       at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
       at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
       at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
       at  oracle.jdbc.driver.OracleStatement.get_internal_type(OracleStatement.java:6433)
       at oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:1354)
    
    0 讨论(0)
  • 2021-01-12 17:43

    You are getting this exception because you are using JdbcTemplate instead of NamedParameterJdbcTemplate. MapSqlParameterSource works with NamedParameterJdbcTemplate.

    0 讨论(0)
  • 2021-01-12 17:45

    You might want to try using strings instead of characters for your CHAR(1) Y/N fields.

    0 讨论(0)
  • 2021-01-12 17:49

    Try changing your code to:

    paramSource.addValue("NAME1",sponsor.getName(), Types.VARCHAR);
    paramSource.addValue("INDUSTRY_TYPE", sponsor.getIndustryType(), Types.INTEGER);
    paramSource.addValue("IS_NOT_SOLICITE", sponsor.getNotSoliciteFlag()?'Y':'N', Types.CHAR);
    paramSource.addValue("IS_REPORTING_SPONSOR", sponsor.getReportingFlag()?'Y':'N', Types.CHAR);
    
    0 讨论(0)
  • 2021-01-12 17:56

    Though it is too late, today I found the same issue. I tried all of the above options which were provided by the other members, but no luck. What I found was, to add a char column in where clause I was providing the java.lang.char in that space.

    Suppose my query looks like this - SELECT * FROM app_config WHERE enable = :enable;, where the 'enable' column is char(1) column in DB. So while passing the parameter in namedparameterJdbcTemplate, I was passing a java char as 'Y', which was causing the error.

    In order to solve that I passed normal java string as the parameter value in the namedparameterJdbcTemplate like "Y" and BANG!!! It works fine. Also I need not to provide the Types in MapSqlParameterSource. So my ultimately code looks like below -

    protected String getConfigValue(String configKey, String enable) throws BusinessException {
            String value = "";
            try {
                if (StringUtils.isNotBlank(configKey)) {
                    MapSqlParameterSource parameters = new MapSqlParameterSource();
                    parameters.addValue(ApplicationDaoConstants.CONFIG_KEY, configKey); // This is the VARCHAR column in DB
                    parameters.addValue(ApplicationDaoConstants.ENABLE, enable); // This is the character column in DB
    
                    value = nmJdbcTmplt.query(AppConfigSQL.GET_CONFIG_VALUE, parameters, rs -> {
                        if (rs.next()) {
                            return rs.getString(ApplicationDaoConstants.CONFIG_VALUE);
                        }
                        return "";
                    });
                }
            } catch (Exception e) {
                log.error("Exception occured while accessing app_config ", e);
                throw new BusinessException(ApplicationConstants.ERROR_ACCESSING_DATABASE);
            }
    
            return value;
        }
    

    I hope this could help others. Thanks!

    0 讨论(0)
  • 2021-01-12 17:57

    In my case I was providing an Enumeration datatype instead of a datatype compatible with VARCHAR2 in database (like String ).

    My query was like :

    private static final String QUERY ="SELECT res.id, FROM result res " +
    "WHERE res.country = :resCountry ";
    

    And at the time of passing the parameter resCountry, I did like this:

    List<Map<String, Object>> listResult = jdbcTemplate.queryForList(QUERY,Country.US);
    

    Now the complaint/exception came from the fact that Country.US was of type Enumeration Country

    I did the following change to make it work.

    List<Map<String, Object>> listResult = jdbcTemplate.queryForList(QUERY,Country.US.name());
    

    After this change it worked fine.

    0 讨论(0)
提交回复
热议问题