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);
}
2
  • 3
    You only have one column in your result set (A.codeA), and you are coding resu.getString(2) Commented May 30, 2018 at 12:42
  • I must do an INNER JOIN ? Because I have 2 tables... Commented May 30, 2018 at 12:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

So, my problem is my request sql ?
No, your request SQL query which select only "one" column is absolutely fine, but the code you've written is trying to access a second column from the query result using resu.getString(2) which isn't present, That is where things are going wrong
I am obliged, to have getString(2) to recuperate the item from my foreign key, I don't understand how to do for now... ;-(
Do you have the same value in your foreign key as well?
my problem is resolve, it was in my class Album the problem; thank you a lot for your help
-1

In my case, while using EclipseLink 3.0.4, the problem was that @MappedSuperclass class contained protected field and inheriting class defined field with the same name. In db there is only one column present with that name.

I am not familiar with how EclipseLink handle Entity fields, however this problem with two same type, same name fields caused this error to be thrown in some cases.

MappedSuperclass:

@MappedSuperclass
public abstract class PersistentObject {

    @Id
    @GeneratedValue
    protected Long id;

    @Column(nullable = false)
    protected boolean alive = true;
}

Inheriting class:

@Entity
public lass MyObj extends PersistentObject {

    @Column(nullable = false)
    private boolean alive = true;
}

2 Comments

This answer is entirely unrelated to the question asked, which is about basic JDBC usage, and does not involve EclipseLink or JPA.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.