Reputation: 23
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.
Upvotes: 0
Views: 139
Reputation: 164099
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);
Upvotes: 2