0

I keep getting the "java.sql.SQLException: Column 'id' not found." when trying select from a table using DB2 on Netbeans. Here is my table creation code:

string="CREATE TABLE PlayerCollection "
                + "(id integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                + "name varchar(20),"
                + "height varchar(20),"
                + "weight varchar(20),"
                + "position varchar(20),"
                + "team varchar(20),"
                + "PRIMARY KEY (id))";

And here is my code to select from the table:

String sql = "select name, height, weight, position, team from PlayerCollection "
                        + "where id=" + strIndex;

Why am I getting this exception even though "id" clearly exists in my table and how do I fix this?
Actually it seems that my error is at this line:

int ind = rs.getInt("id");

Here "rs" is a result set.

1
  • Actually it seems that my error is at this line "int ind = rs.getInt("id");" Here "rs" is a result set. Commented Dec 14, 2019 at 18:47

1 Answer 1

2

The resultset contains only the columns that you select, so you must include the column id in the select list:

String sql = "select id, name, height, weight, position, team from PlayerCollection "
           + "where id=" + strIndex;

or since you have the id's value in the variable strIndex which you use in the WHERE clause, simply remove the line:

int ind = rs.getInt("id");

from your code, or change it to this:

int ind = Integer.parseInt(strIndex);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.