简体   繁体   English

java.sql.SQLException:无效的列索引

[英]java.sql.SQLException: Invalid column index

I have a SQL query which I want to use to count components into table. 我有一个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),
                        ...............

Here is the complete source code: http://pastebin.com/YMvqBPpV 这是完整的源代码: http : //pastebin.com/YMvqBPpV

I get this error message: java.sql.SQLException: Invalid column index 我收到此错误消息:java.sql.SQLException:无效的列索引

Is this design problem or the problem is into the SQL query? 是这个设计问题还是问题进入了SQL查询?

I saw your full source code and you have a bunch of result.getInt(INDEX) . 我看到了完整的源代码,并且您有一堆result.getInt(INDEX) Since you are only doing SELECT COUNT(1) , there is only one column, so for any value of INDEX other than 1, the getInt() will fail. 由于您只执行SELECT COUNT(1) ,所以只有一列,因此对于INDEX值(非1), getInt()将失败。

Change your query to SELECT <LIST> ... where LIST is a comma separated list of column names from which you want to retrieve values. 将查询更改为SELECT <LIST> ... ,其中LIST是要从中检索值的列名的逗号分隔列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM