从表中选择DB2获得异常

问题描述 投票:-1回答:1

我一直在获取“ 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"是结果集。

java jdbc netbeans db2 sqlexception
1个回答
1
投票

结果集仅包含您选择的列,因此您必须在选择列表中包括列id

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

或者因为您在id子句中使用的变量strIndex中具有WHERE的值,所以只需删除该行:

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

从您的代码,或将其更改为此:

int ind = Integer.parseInt(strIndex);
© www.soinside.com 2019 - 2024. All rights reserved.