我不断收到“java.sql.SQLException:未找到列 'id'”。当尝试使用 Netbeans 上的 DB2 从表中进行选择时。这是我的表创建代码:
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))";
这是我从表中选择的代码:
String sql = "select name, height, weight, position, team from PlayerCollection "
+ "where id=" + strIndex;
即使“id”明显存在于我的表中,为什么我会收到此异常?如何解决此问题?
实际上我的错误似乎是在这一行:
int ind = rs.getInt("id");
这里“rs”
是一个结果集。
最佳答案
结果集仅包含您选择的列,因此您必须在选择列表中包含列 id
:
String sql = "select id, name, height, weight, position, team from PlayerCollection "
+ "where id=" + strIndex;
或者由于您在 WHERE
子句中使用的变量 strIndex
中有 id
的值,因此只需删除该行:
int ind = rs.getInt("id");
从您的代码中,或将其更改为:
int ind = Integer.parseInt(strIndex);
关于java - 使用 DB2 Select from Table 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59337997/