Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

mysql - java.sql.SQLException: Column Index out of range, 8 > 6

When I try retrieve the whole record and display it from database. Its showing an error

java.sql.SQLException: Column Index out of range, 8 > 6. 

I am not able to figure this out. pls help.

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 WHERE I AM TRYING TO SHOW THE RESULT

<%
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&nbsp;&nbsp;</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)%>&nbsp;</div></td>
    <td><div align="center"><%=(String)one.get(5)%>&nbsp;</div></td>

</tr>
<% } %>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This error implies that your adddriver only has 6 columns, so 8 is an invalid column index.

This means all of these statements have invalid indices :

            one.add(rs.getString(8));
            one.add(rs.getString(9));
            one.add(rs.getString(10));
            one.add(rs.getInt(11));

Perhaps your DB table doesn't contain what you think it does.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...