我在向数据库插入行时使用下面的代码(oracle 10g xe,jar:ojdbc14.jar)
String sql = "INSERT INTO SPONSOR_TB(ID,NAME,INDUSTRY_TYPE,IS_REPORTING_SPONSOR,IS_NOT_SOLICITE) VALUES(SEQ_SPONSOR_ID.NEXTVAL,:NAME1,:INDUSTRY_TYPE,:IS_REPORTING_SPONSOR,:IS_NOT_SOLICITE)";
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("NAME1",sponsor.getName());
paramSource.addValue("INDUSTRY_TYPE", sponsor.getIndustryType());
paramSource.addValue("IS_NOT_SOLICITE", sponsor.getNotSoliciteFlag()?'Y':'N');
paramSource.addValue("IS_REPORTING_SPONSOR", sponsor.getReportingFlag()?'Y':'N');
KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update(sql, paramSource, generatedKeyHolder,new String[]{"ID"});
int id = generatedKeyHolder.getKey().intValue();
表的结构是:
create table SPONSOR_TB
(
id INTEGER not null,
name VARCHAR2(20),
industry_type INTEGER not null,
is_reporting_sponsor CHAR(1) not null,
is_not_solicite CHAR(1) not null
)
和SEQ_SPONSOR_ID是序列
赞助商类是:
public class Sponsor{
private int id;
private String name;
private boolean reportingFlag;
private boolean notSoliciteFlag;
private int industryType;
//getter setter
}
spring-servlet.xml中的db配置是:
<beans:bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<beans:property
name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<beans:property
name="username"
value="SPONSOR_DB" />
<beans:property
name="password"
value="ajeet" />
<beans:property
name="url"
value="jdbc:oracle:thin:@localhost:1521:XE" />
</beans:bean>
我遇到以下异常:
SEVERE: Servlet.service() for servlet [spring] in context with path [/GroupSolution] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO SPONSOR_TB (ID,NAME,INDUSTRY_TYPE,IS_REPORTING_SPONSOR,IS_NOT_SOLICITE) VALUES(SEQ_SPONSOR_ID.NEXTVAL,?,?,?,?)]; SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type] with root cause
java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:9168)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8749)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9471)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:9454)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:127)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.setValues(PreparedStatementCreatorFactory.java:298)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:251)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:581)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:843)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:288)
at com.groupsolution.dao.SponsorDaoImpl.createSponsor(SponsorDaoImpl.java:55)
at com.groupsolution.service.SponsorServiceImpl.createSponsor(SponsorServiceImpl.java:31)
at com.groupsolution.controller.SponsorController.addSponsor(SponsorController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:5)
您可能想尝试在CHAR(1)Y / N字段中使用字符串而不是字符。
答案 1 :(得分:4)
您收到此异常是因为您使用的是JdbcTemplate
而不是NamedParameterJdbcTemplate
。 MapSqlParameterSource
与NamedParameterJdbcTemplate
一起使用。
答案 2 :(得分:2)
尝试将代码更改为:
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);
答案 3 :(得分:0)
对我来说,当我尝试在timestamp列上设置null值时,会出现此问题。我更改了值timestamp(0)。这对我来说似乎是一个驱动问题。我使用的是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)
答案 4 :(得分:0)
在我的情况下,我提供的是Enumeration数据类型,而不是与数据库中的VARCHAR2兼容的数据类型(例如String)。
我的查询就像:
private static final String QUERY ="SELECT res.id, FROM result res " +
"WHERE res.country = :resCountry ";
在传递参数resCountry时,我确实是这样的:
List<Map<String, Object>> listResult = jdbcTemplate.queryForList(QUERY,Country.US);
现在,投诉/例外来自Country.US类型为枚举国家/地区
的事实我进行了以下更改以使其正常工作。
List<Map<String, Object>> listResult = jdbcTemplate.queryForList(QUERY,Country.US.name());
此更改后效果很好。
答案 5 :(得分:0)
尽管为时已晚,今天我发现了同样的问题。我尝试了其他成员提供的所有上述选项,但是没有运气。我发现的是,在其中要提供java.lang.char的where子句中添加char列。
假设我的查询看起来像这样-SELECT * FROM app_config WHERE enable = :enable;
,其中“启用”列是数据库中的char(1)列。因此,当在namedparameterJdbcTemplate中传递参数时,我将Java字符传递为'Y'
,这引起了错误。
为了解决这一问题,我在"Y"
和BANG之类的namedparameterJdbcTemplate中传递了普通的Java字符串作为参数值!它工作正常。另外,我不需要在MapSqlParameterSource中提供Types
。所以我的最终代码如下所示-
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;
}
我希望这可以帮助其他人。 谢谢!