0

我有一个 SQL 查询,我想用它来将组件计数到表中。

private DCDataObj dc;

    public class DCDataObj
    {

        private int datacenter;             //  Datacenters
        ..............

        public DCDataObj(int datacenter............)
        {
            this.datacenter = datacenter;
            ...............
        }

        public int getDatacenter()
        {
            return datacenter;
        }

        public void setDatacenter(int datacenter)
        {
            this.datacenter = datacenter;
        }

        ............
    }

ps = conn.prepareStatement("SELECT COUNT(1) AS CNT FROM COMPONENTSTATS CS, COMPONENTTYPE CT "
        + " WHERE CS.COMPONENTTYPEID = CT.COMPONENTTYPEID AND CT.COMPONENTTYPEID IN ( "
        + " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " //  10
        + " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " //  20
        + " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " //  30
        + " ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) " //  40
        + " GROUP BY CT.NAME ORDER BY CT.NAME");

ps.setInt(1, 1000);
...............

ResultSet result = ps.executeQuery();
            while (result.next())
            {

                dc = new DCDataObj(
                        result.getInt(1),
                        ...............

这是完整的源代码: http: //pastebin.com/YMvqBPpV

我收到此错误消息:java.sql.SQLException: Invalid column index

这是设计问题还是问题出在 SQL 查询中?

4

1 回答 1

1

我看到了你的完整源代码,你有一堆result.getInt(INDEX). 由于您只在做SELECT COUNT(1),因此只有一列,因此对于INDEX1 以外的任何值,getInt()都会失败。

将查询更改为要从中检索值的列名的逗号分隔列表SELECT <LIST> ...在哪里。LIST

于 2013-02-26T20:55:04.487 回答