当我尝试检索整个记录并从数据库中显示它时。它显示错误
java.sql.SQLException: Column Index out of range, 8 > 6.
我无法弄清楚这一点。请帮助。
JAVA CODE
public ArrayList viewAllDrivers() {
ArrayList allDrivers=new ArrayList();
try {
String sql= "select * from adddriver ORDER BY dname";
rs =DBConnection.executeQuery(sql);
while(rs.next()) {
ArrayList one = new ArrayList();
one.add(rs.getInt(1));
one.add(rs.getString(2));
one.add(rs.getString(6));
one.add(rs.getString(8));
one.add(rs.getString(9));
one.add(rs.getString(10));
one.add(rs.getInt(11));
allDrivers.add(one);
}
}
catch (Exception ex) {
System.out.println (ex);
}
return allDrivers;
}
PAGE我正在尝试显示结果
<%
SearchDAO searchDAO = new SearchDAO();
ArrayList all = searchDAO.viewAllDrivers();
int size = all.size();
%>
<table width="95%" align="center" style="border:#D22929 solid 2px;padding:10px;" border="0">
<tr>
<th bgcolor="#D22929" scope="col"><span class="style10">Driver Name </span></th>
<th bgcolor="#D22929" scope="col"><span class="style10">Address</span></th>
<th bgcolor="#D22929" scope="col"><span class="style10">City</span></th>
<th bgcolor="#D22929" scope="col"><span class="style10">Contact</span></th>
<th bgcolor="#D22929" scope="col"><span class="style10">Country </span></th>
<th bgcolor="#D22929" scope="col"><span class="style10">Ation</span></th>
</tr>
<%
for(int i=0;i<size;i++){
ArrayList one=(ArrayList)all.get(i);
%>
<tr style="height:30px; padding:4px;">
<td><div align="center"><%=(String)one.get(1)%></div></td>
<td><div align="center"><%=(String)one.get(2)%></div></td>
<td><div align="center"><%=(String)one.get(3)%></div></td>
<td><div align="center"><%=(String)one.get(4)%> </div></td>
<td><div align="center"><%=(String)one.get(5)%> </div></td>
</tr>
<% } %>
答案 0 :(得分:1)
此错误表示您的adddriver
只有6列,因此8是无效的列索引。
这意味着所有这些语句都有无效索引:
one.add(rs.getString(8));
one.add(rs.getString(9));
one.add(rs.getString(10));
one.add(rs.getInt(11));
也许你的数据库表并不包含你的想法。
答案 1 :(得分:1)
总是更好地明确命名您要检索的列,这样您就不会遇到这样的问题。
public ArrayList viewAllDrivers(){ ArrayList allDrivers = new ArrayList();
try {
String sql= "select city,address,... from adddriver ORDER BY dname";
rs =DBConnection.executeQuery(sql);
while(rs.next()) {
ArrayList one = new ArrayList();
one.add(rs.getInt(1));
one.add(rs.getString(2));
one.add(rs.getString(6));
one.add(rs.getString(8));
one.add(rs.getString(9));
one.add(rs.getString(10));
one.add(rs.getInt(11));
allDrivers.add(one);
}
}
catch (Exception ex) {
System.out.println (ex);
}
return allDrivers;
}
由于您遇到此异常
,因此您的列数已经过时了