数字格式化大整数的异常

huangapple go评论84阅读模式
英文:

NumberFormatting exception for bigint

问题

我正在使用PostgreSQL作为数据库,在Spring JdbcTemplate中连接到数据库。

数据库中的电话列为phone bigint。但是Spring抛出了java.lang.NumberFormatException错误。

            public UserDetails getUserDetails(int phone) {
    
    		String sql = "SELECT * FROM users where phone = ?";
    		UserDetails users = (UserDetails) jdbcTemplate.query(sql,new Object[]{phone},
    				new BeanPropertyRowMapper<UserDetails>(UserDetails.class));
    		return users;
    	    }

**错误-**

> 输入字符串为"7894561230"嵌套异常是
> java.lang.NumberFormatException输入字符串为"7894561230"

我的Bean类是:

    public class UserDetails {
    	
    	private int userId;
    	private String name;
    	private String email;
    	private String phone;
    }
英文:

I am using database as postgresql, connecting to the database with spring jdbctemplate.

The phone column in database is phone bigint The spring is throwing the java.lang.NumberFormatException

            public UserDetails getUserDetails(int phone) {
    
    		String sql = &quot;SELECT * FROM users where phone = ?&quot;;
    		UserDetails users = (UserDetails) jdbcTemplate.query(sql,new Object[]{phone},
    				new BeanPropertyRowMapper&lt;UserDetails&gt;(UserDetails.class));
    		return users;
    	    }

**Error:-**

&gt; For input string: &quot;7894561230&quot;; nested exception is
&gt; java.lang.NumberFormatException: For input string: &quot;7894561230&quot;

My Bean is :-

    public class UserDetails {
    	
    	private int userId;
    	private String name;
    	private String email;
    	private String phone;
    }

答案1

得分: 0

你的问题出在 int 上。

在Java中,int 可以容纳从 -21474836482147483647 的任何整数,而你的值 7894561230 超出了其范围。因此请使用 long 替代 int

英文:

You problem is with int.

In Java int can hold any whole number from -2147483648 to 2147483647 and your value 7894561230 is out of range for it. So use long in place of int.

huangapple
  • 本文由 发表于 2020年8月21日 18:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定