MySQLDataTruncation:列超出范围值

Dav*_*vid 3 java mysql ibatis

我正在使用ibatis和java webapp插入一行.POJO有一个需要存储数字的字段(如3.0或2.34).我已尝试在java端使用BigDecimal和Double.在MySQL方面,我使用的是十进制(5,5)数据类型.

当我尝试插入一个"4"作为此数字字段的值的行时,MySQL和iBatis抛出以下异常:

org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation: encountered SQLException [  
--- The error occurred in org/mySQL.xml.  
--- The error occurred while applying a result map.  
--- Check the mySQL.insertQuery.  
--- The error happened while setting a property on the result object.  
--- Cause: java.lang.RuntimeException: org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation: encountered SQLException [  
--- The error occurred while applying a parameter map.  
--- Check the insertQuery-InlineParameterMap.  
--- Check the statement (update failed).  
--- Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'numericColumn' at row 316]; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
    at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)
    at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithRowHandler(GeneralStatement.java:133)
    at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryWithRowHandler(SqlMapExecutorDelegate.java:649)
    at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryWithRowHandler(SqlMapSessionImpl.java:156)
    at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryWithRowHandler(SqlMapClientImpl.java:133)
    at org.springframework.orm.ibatis.SqlMapClientTemplate$5.doInSqlMapClient(SqlMapClientTemplate.java:267)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:165)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.queryWithRowHandler(SqlMapClientTemplate.java:265)
    at org.myClass(myClass.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:248)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:165)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:66)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:191)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:516)
Run Code Online (Sandbox Code Playgroud)

我是否使用错误的java数据类型来插入值?如果没有,为什么MySQL不允许我插入值?

Clo*_*use 7

你似乎误解了如何定义一个DECIMAL(或潜在NUMERIC的SQL)柱工程-
具体而言,定义列时DECIMAL(x, y),x的数字列将存储号码,y是的位数的小数点.因此,在列定义中,您指定了-1 <column <1的范围.数据类型是正确的,但您尝试指定列被告知允许的范围之外的值.幸运的是,修复很简单:更改列定义.我假设您希望在小数点之前最多有5位数字,这将使列定义DECIMAL(10, 5).