javasqljdbc

java.sql.SQLException: Column Index out of range, 2 > 1


I have an error message that I don't understand. The message is the next

java.sql.SQLException: Column Index out of range, 2 > 1

Does the problem come from my request SQL?

String req = "Select A.CodeA from album A, collabo C where A.CodeA = C.CodeA order by 1 ";
ResultSet resu = ConnexionMySQL.getInstance().selectQuery (req);
try {
    while (resu.next())
    {  
        myList.add (new Appareil(resu.getString(1), 
                     new Album (resu.getString(2))));
                     
     }
}

Or perhaps in my file TableModel Appareil? I have a column "Identification" only, I don't understand why it doesn't work.

private String[] columnNames = {"Identification"};
private ArrayList <Appareil> myList;

public TableModelAppareils (ArrayList myList)
{
    this.myList = myList;
}

public int getColumnCount() {
    return columnNames.length;
}

public int getRowCount() {
    //System.out.println("row count : " + myList.size());
    return myList.size();
}

@Override
public String getColumnName(int col) {
    return columnNames[col];
}

@Override
public Object getValueAt(int row, int col) {
    Appareil myApp = myList.get(row);
    switch (col)
    {
        case 0 :    return myApp.getAppAlb().getCodeA();
    }
    return null;
}

@Override
public Class getColumnClass(int c) {
    switch (c)
    {
        case 0 :    return String.class;
       
    }
    return null;
}

public void setMyList (ArrayList myList)
{
    this.myList = myList;
    this.fireTableDataChanged();
}

public ArrayList <Appareil> getMyList ()
{
    return myList;
}

public Appareil getMyList (int index)
{
    return myList.get(index);
}

Solution

  • You are accessing a second column from ResultSet using resu.getString(2) in your code however you're just selecting one column A.codeA in your select query