package BankClient;import java.sql.*;public class DBManage {
Statement sta = null;
ResultSet res = null; public boolean userTest(String sql) {
try {
sta = DBConnect.getDBConnect().createStatement();
res = sta.executeQuery(sql);
if (res.next()) {
return true;
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
      try{
if(sta!= null){
  sta.close();
  sta=null;
}
if(res!=null){
  res.close();
  res=null;
}
      }catch(SQLException e){
       e.printStackTrace();
      }
      


} return false; }
public  List showTable(){
List list = new Vector();

try {

sta =DBConnect.getDBConnect().createStatement();
res=sta.executeQuery("show tables");

while(res.next()){

list.add(res.getString(1));

    
}

  sta.close();
  res.close();

} catch (SQLException e) {

e.printStackTrace();
}
finally{
      try{
if(sta!= null){
  sta.close();
  sta=null;
}
if(res!=null){
  res.close();
  res=null;
}
      }catch(SQLException e){
       e.printStackTrace();
      }

}

return list;


}
public static void main(String[] args) {
List a= new DBManage().showTable();
for (int i = 0; i <= a.size(); i++) {
System.out.println(a.get(i));

}}
}为什么会出现数组越界啊  烦躁

解决方案 »

  1.   

    arraylist 索引从0开始的,所以最大的是 a.size() -1;你要改成 for (int i = 0; i < a.size(); i++)
    List a= new DBManage().showTable();
    for (int i = 0; i <= a.size(); i++) {
    System.out.println(a.get(i)); 
    }
      

  2.   

    你的主方法里出错了
    public static void main(String[] args) {
    List a= new DBManage().showTable();
    for (int i = 0; i <= a.size(); i++) {
    System.out.println(a.get(i));
    } }i从0取,所有i<a.size();不能有等于。
    改成:
    public static void main(String[] args) {
    List a= new DBManage().showTable();
    for (int i = 0; i < a.size(); i++) {
    System.out.println(a.get(i));
    } }则可
      

  3.   

    呵呵,错误太明显了啊, 
    for (int i = 0; i <= a.size(); i++) 
      

  4.   

    这种问题u 最好从IDE报错中,能看出什么错误!!这种越界的很明显!!
    lz要加油哦!!!
    Eclipse的自动纠错功能很强大
      

  5.   

    楼上几位已经说得很明白了,我觉得还有一个问题:public boolean userTest(String sql) { 
    try { 
        sta = DBConnect.getDBConnect().createStatement(); 
        res = sta.executeQuery(sql); 
        if (res.next()) { //在这个地方结果集已经下移了一个
            return true; 
        }
    res=sta.executeQuery("show tables"); 
    while(res.next()){ //这里又走了一次
      

  6.   

    主要是要看Exception显示什么错.这个地方while(res.next()){作为判断应该用while(res.hasNext()){
      

  7.   

    主要是要看Exception显示什么错.这个地方while(res.next()){作为判断应该用while(res.hasNext()){