Eclipse中main函数由控制台输入参数为空的处理

@简单的main函数:实现公司编码缺省时,这里将默认取所有公司编码
import java.sql.SQLException;
import com.evsoft.db.DataList;
import com.evsoft.db.Db;
public class teststr {
    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {        
        DataList gsbmList=null;        
        // TODO Auto-generated method stub
        String sql=" select distinct col_1_1_1_3 as gsbm from 公司名称静态表  where 1=1 ";
        if(args.length>0){        
            sql=sql+" and col_1_1_1_3 in("+args[0]+")";    
        }
        gsbmList=Db.selectArrayList(sql);    
        while(gsbmList.next()){
            gsbmList.getString("gsbm");    
            System.out.println(gsbmList.getString("gsbm"));
       }
        System.out.println(sql);
    }
}

@注意:这里若用null来判断即 args[0]!=null,那么将报数组越界异常

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at com.evsoft.test.teststr.main(teststr.java:16)

原文地址:https://www.cnblogs.com/zhutouying/p/3121526.html