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));
}}
}为什么会出现数组越界啊 烦躁
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));
}}
}为什么会出现数组越界啊 烦躁
List a= new DBManage().showTable();
for (int i = 0; i <= a.size(); i++) {
System.out.println(a.get(i));
}
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));
} }则可
for (int i = 0; i <= a.size(); i++)
lz要加油哦!!!
Eclipse的自动纠错功能很强大
try {
sta = DBConnect.getDBConnect().createStatement();
res = sta.executeQuery(sql);
if (res.next()) { //在这个地方结果集已经下移了一个
return true;
}
res=sta.executeQuery("show tables");
while(res.next()){ //这里又走了一次