データベースに行を挿入するときに以下のコードを使用しています(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)
CHAR(1)Y/Nフィールドには、文字の代わりに文字列を使用してみてください。
JdbcTemplate
の代わりにNamedParameterJdbcTemplate
を使用しているため、この例外が発生します。 MapSqlParameterSource
はNamedParameterJdbcTemplate
と連携します。
コードを次のように変更してみてください。
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);
私の場合、この問題は、タイムスタンプ列に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)