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)
------java.lang.NumberFormatException: For input string 解决方法--------------------------------------------------------
System.out.println(Integer.parseInt(gmk.getKicid()));
数值型的, 传入空格字符串不异常就鬼了.
------解决方法--------------------------------------------------------
max(kicid)---返回的不是字符型!
------解决方法--------------------------------------------------------
For input string: "9572032 "
后面有几个空格,看到吧
------解决方法--------------------------------------------------------
加个空字符窜就不会了
System.out.println( " "+Integer.parseInt(gmk.getKicid()));
------解决方法--------------------------------------------------------
Integer.parseInt,参数只能是纯数字的组合才能转换,你的For input string: "9572032 "
里面有空格了,你如果非要这么用,就trim一下吧