package mypackage;
import java.sql.*;
public class GetMaxKic {
String kicid;
Connection con;
Statement stmt;
ResultSet set;
public GetMaxKic()
{
this.kicid=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.con=DriverManager.getConnection("jdbc:odbc:xkyy","sa","sa");
String sql="select max(kicid) from l_bio_brxxk";
this.stmt=this.con.createStatement();
set=stmt.executeQuery(sql);
while(set.next())
{
this.kicid=set.getString(1);
}

catch(Exception e)
{
e.printStackTrace();
}
}
public String getKicid()
{
return this.kicid;
}
public static void main(String[] args)
{
GetMaxKic gmk=new GetMaxKic();
System.out.println(gmk.getKicid());
System.out.println(Integer.parseInt(gmk.getKicid()));
}
出现的是这样的异常,我是用eclipse编译器.第一个打印语句没问题,到了第二个打印语句就有异常.
异常如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "9572032   "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at mypackage.GetMaxKic.main(GetMaxKic.java:36)

解决方案 »

  1.   

    System.out.println(Integer.parseInt(gmk.getKicid()));
    数值型的, 传入空格字符串不异常就鬼了.
      

  2.   

    max(kicid)---返回的不是字符型!
      

  3.   

    For input string: "9572032   "
    后面有几个空格,看到吧
      

  4.   

    加个空字符窜就不会了
    System.out.println(""+Integer.parseInt(gmk.getKicid()));
      

  5.   

    Integer.parseInt,参数只能是纯数字的组合才能转换,你的For input string: "9572032   "
    里面有空格了,你如果非要这么用,就trim一下吧
      

  6.   

    System.out.println(Integer.parseInt(gmk.getKicid().trim()));
      

  7.   

    API贴上,最后一句Parameters:
    s the String containing the integer representation to be parsed
    radix the radix to be used while parsing s.
    Returns:
    the integer represented by the string argument in the specified radix.
    Throws:
    NumberFormatException if the String does not contain a parsable int.