java.sql.SQLException: Value ‘320123456789012345‘ is outside of valid range for type java.lang.Integ

问题

笔者使用JDBC连接数据库,查询数据是控制台报错

java.sql.SQLException: Value '320123456789012345' is outside of valid range for type java.lang.Integer Query: SELECT * FROM UserInfo WHERE uId=? Parameters: [1]

详细问题

java.sql.SQLException: Value ‘320123456789012345‘ is outside of valid range for type java.lang.Integ_第1张图片
笔者Java实体类内容

package com.iflytek.bms.domain;

import lombok.Data;

@Data
public class UserInfo {

    private Integer id;

    private Integer uId;

    private Integer idCard;

    private String fullName;

    private String email;

    private String phone;

    private String address;

}

笔者UserInfoDaoImpl Class文件实现查询函数如下

    @Override
    public UserInfo selectUserInfoByUId(Integer uId) {
        System.out.println("uId" + uId);
        String sql = "SELECT * FROM UserInfo WHERE uId=?";
        try {
                return runner.query(sql, new BeanHandler<>(UserInfo.class), 1);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

控制台详细报错

uId1
java.sql.SQLException: Value '320123456789012345' is outside of valid range for type java.lang.Integer Query: SELECT * FROM UserInfo WHERE uId=? Parameters: [1]
	at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:527)
	at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:391)
	at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:329)
	at com.iflytek.bms.dao.impl.UserInfoDaoImpl.selectUserInfoByUId(UserInfoDaoImpl.java:76)
	at com.iflytek.bms.web.listener.admin.AdminUserListServlet.doGet(AdminUserListServlet.java:43)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:670)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.iflytek.bms.web.filter.LoginFilter.doFilter(LoginFilter.java:33)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at com.iflytek.bms.web.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:26)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

解决方案

修改Java实体类内容中的idCard属性类型,将其改为String

解决原因

错误原因是数据库中字段与实体类中的字段不对应。
具体的:笔者问题的原因是在Java实体类UserInfo中,将idCard属性的类型定义为Integer,而数据库表中对应的字段类型是varchar,这导致在查询时无法将字符串类型的数据转换为整数类型。
解决这个问题的方法是将idCard属性的类型改为String,以便与数据库表中的字段类型匹配。修改UserInfo实体类的代码如下:通过将idCard属性的类型改为String,就可以正确地将数据库中的varchar类型的数据映射到Java实体类中。这样,在执行查询操作时就不会再出现类型转换错误。
请注意,当将idCard属性的类型修改为String后,需要相应地修改代码中对idCard属性的处理逻辑,以适应新的类型。

总结

笔者认为这个报错似乎对开发者并不友好,似乎是告诉开发者传递的参数值为 ‘320123456789012345’,笔者调试后发现传递的参数值非’320123456789012345’,参考该帖子mybatis查询错误:’Value ‘125878855456545’ is outside of valid range for type java.lang.Integer;发现错误原因是数据库中字段与实体类中的字段不对应。

参考文献

mybatis查询错误:’Value ‘125878855456545’ is outside of valid range for type java.lang.Integer;
chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

你可能感兴趣的:(java,servlet,开发语言)