使用 DB2 Select 从 Table 获取异常
Getting Exception With DB2 Select from Table
在 Netbeans 上使用 DB2 从 table 尝试 select 时,我一直得到 "java.sql.SQLException: Column 'id' not found."。这是我的 table 创建代码:
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))";
这是我从 table 到 select 的代码:
String sql = "select name, height, weight, position, team from PlayerCollection "
+ "where id=" + strIndex;
即使 "id" 明显存在于我的 table 中,为什么我会收到此异常?我该如何解决这个问题?
实际上我的错误似乎是在这一行:
int ind = rs.getInt("id");
这里"rs"
是一个结果集
结果集仅包含您 select 的列,因此您必须在 select 列表中包含 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);
在 Netbeans 上使用 DB2 从 table 尝试 select 时,我一直得到 "java.sql.SQLException: Column 'id' not found."。这是我的 table 创建代码:
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))";
这是我从 table 到 select 的代码:
String sql = "select name, height, weight, position, team from PlayerCollection "
+ "where id=" + strIndex;
即使 "id" 明显存在于我的 table 中,为什么我会收到此异常?我该如何解决这个问题?
实际上我的错误似乎是在这一行:
int ind = rs.getInt("id");
这里"rs"
是一个结果集
结果集仅包含您 select 的列,因此您必须在 select 列表中包含 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);