java值byte類型長度超出

本人遇到的問題是mysql字段定義的是tinyint(3) unsigned  對應的java裏面的是byte。 

一、遇到的問題是這樣的:

[RemoteTestNG] detected TestNG version 6.9.10
log4j: Parsing for [root] with value=[DEBUG,D,E,stepLog].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "D".
log4j: Parsing layout options for "D".
log4j: Setting property [conversionPattern] to [%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n].
log4j: End of parsing for "D".
log4j: Setting property [append] to [true].
log4j: Setting property [threshold] to [DEBUG].
log4j: Setting property [file] to [logs/logs.log].
log4j: setFile called: logs/logs.log, true
log4j: setFile ended
log4j: Appender [D] to be rolled at midnight.
log4j: Parsed "D" options.
log4j: Parsing appender named "E".
log4j: Parsing layout options for "E".
log4j: Setting property [conversionPattern] to [%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n].
log4j: End of parsing for "E".
log4j: Setting property [append] to [true].
log4j: Setting property [threshold] to [ERROR].
log4j: Setting property [file] to [logs/error.log].
log4j: setFile called: logs/error.log, true
log4j: setFile ended
log4j: Appender [E] to be rolled at midnight.
log4j: Parsed "E" options.
log4j: Parsing appender named "stepLog".
log4j: Parsing layout options for "stepLog".
log4j: Setting property [conversionPattern] to [%m%n].
log4j: End of parsing for "stepLog".
log4j: Setting property [file] to [logs/stepLog.log].
log4j: Setting property [append] to [true].
log4j: Setting property [threshold] to [INFO].
log4j: setFile called: logs/stepLog.log, true
log4j: setFile ended
log4j: Appender [stepLog] to be rolled at midnight.
log4j: Parsed "stepLog" options.
log4j: Finished configuring.
[TestNG] Running:
C:\Users\lenovo\AppData\Local\Temp\testng-eclipse-1408996642\testng-customsuite.xml

Begin executing test method[com.xueersi.qa.continuation.testcase.CouponCouponGetCouponListTest . Coupon_Coupon_getCouponList_test ()] in thread[1].

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1877986974.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6fefce9e]
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'type' from result set. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '255' in column '4' is outside valid range for the datatype TINYINT.
### The error may exist in com/xes/qa/apitest/jcsj/db/mapper/Xes_couponsMapper.java (best guess)
### The error may involve com.xes.qa.apitest.jcsj.db.mapper.Xes_couponsMapper.getcouponList
### The error occurred while handling results
### SQL: SELECT id,name,code,type,valid_start_date,valid_end_date,face_value,order_full_price,receive_type,num,receive_start_time,receive_stime,receive_end_time,receive_etime, `status`,description,creater_id,create_time,modify_time FROM xes_coupons WHERE `receive_type` = 7 AND `status` = 3 AND `valid_start_date` <= SYSDATE() AND `valid_end_date` >= SYSDATE() AND `receive_stime` <= unix_timestamp(now()) AND `receive_etime` >= unix_timestamp(now()) ORDER BY `face_value` DESC, `id` DESC;
### Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'type' from result set. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '255' in column '4' is outside valid range for the datatype TINYINT.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy25.getcouponList(Unknown Source)
at com.xes.qa.apitest.jcsj.db.dbutils.DbUtils.getXesCoupuonsList(DbUtils.java:43)
at com.xueersi.qa.continuation.testcase.CouponCouponGetCouponListTest.Coupon_Coupon_getCouponList_test(CouponCouponGetCouponListTest.java:115)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:197)
at org.simfast.simpletester.unitils.SimpleTesterTestNG.run(SimpleTesterTestNG.java:244)
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:209)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'type' from result set. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '255' in column '4' is outside valid range for the datatype TINYINT.
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:68)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyAutomaticMappings(DefaultResultSetHandler.java:434)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:344)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:298)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:273)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:246)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:160)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:78)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:303)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:154)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:102)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120)
... 34 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '255' in column '4' is outside valid range for the datatype TINYINT.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
at com.mysql.jdbc.ResultSetImpl.throwRangeException(ResultSetImpl.java:7091)
at com.mysql.jdbc.ResultSetImpl.getByteFromString(ResultSetImpl.java:1712)
at com.mysql.jdbc.ResultSetImpl.getByte(ResultSetImpl.java:1659)
at com.mysql.jdbc.ResultSetImpl.getByte(ResultSetImpl.java:1671)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:68)
at com.sun.proxy.$Proxy28.getByte(Unknown Source)
at org.apache.ibatis.type.ByteTypeHandler.getNullableResult(ByteTypeHandler.java:37)
at org.apache.ibatis.type.ByteTypeHandler.getNullableResult(ByteTypeHandler.java:26)
at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:66)
... 48 more
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6fefce9e]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6fefce9e]
Returned connection 1877986974 to pool.
[Failed] CouponCouponGetCouponListTest.Coupon_Coupon_getCouponList_test(org.simfast.simpletester.core.TestData)[pri:0, instance:com.xueersi.qa.continuation.testcase.CouponCouponGetCouponListTest@5e316c74]
Report directory is :D:\sourcecode\new-test\target\SimpleTester
PooledDataSource forcefully closed/removed all connections.
FAILED: Coupon_Coupon_getCouponList_test(org.simfast.simpletester.core.TestData@d278d2b)
java.lang.NullPointerException
at com.xueersi.qa.continuation.testcase.CouponCouponGetCouponListTest.Coupon_Coupon_getCouponList_test(CouponCouponGetCouponListTest.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:197)
at org.simfast.simpletester.unitils.SimpleTesterTestNG.run(SimpleTesterTestNG.java:244)
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:209)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@77b52d12: 6 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@15975490: 5 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4157f54e: 3 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@735f7ae5: 16 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2c6a3f77: 19 ms

之前調通的代碼一運行突然報了這樣一堆的錯,出錯了不要緊看下問題出在哪裏,於是仔細觀察一番之後發現了這一堆報錯中的關鍵信息,如下:

 

確定問題之後就到代碼中去看看唄,代碼如下:

mapper裏面配置的代碼如下:

 

model裏面對應數據庫字段的代碼如下:

看來看去很簡單的代碼嘛,沒什麼錯,這時候想到去數據庫裏看看吧

詢問研發老師,果然這裏的字段他們調整了,增加了101,102,255,看到255再回去看報錯也是跟255有關係的,基本可以確定問題引發的原因了。

那麼怎麼解決呢?

二、解決辦法


要解決問題得先理解下錯誤的具體原因:

1. 網上查詢資料對jdbcType這個參數的描述不多,作用不清楚,大概的意思是“jdbcType這個參數的應用場景就是,當執行mapping文件的時候,有個映射的參數爲空,那麼無法確定他的類型,這個時候就需要jdbcType來確定類型“。

2. 既然jdbc Type是備用的那就不考慮調整了,於是解決思路放到了model裏面的java代碼上,獲取到的資料如下:

    mysql中的tinyint和java中的byte的數值範圍是一樣的(-128~127),如果你能保證自己存儲的數值不會超過這個最大值是可以的,而且tinyint只有1字節。

   既然數據庫使用tinyint,肯定取值在tinyint的範圍內。結果映射的時候是可以指定javaType的。 沒必要使用默認的byte,完全可以用int,Integer。

 看到這裏基本就確定了問題的解決辦法,其實就是byte類型是-128~127,而開發在庫中新加入的255超出了這個範圍所以導致出錯,因此修改model代碼如下:

 

調整完成再運行,果然問題解決了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章