0

I have a SQL query which I want to use to count components into table.

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

I get this error message: java.sql.SQLException: Invalid column index

Is this design problem or the problem is into the SQL query?

2
  • 1
    Are you getting the error at ps.setInt or result.getInt? Commented Feb 26, 2013 at 20:52
  • The full stacktrace should show you the line on which the problem is. Commented Feb 26, 2013 at 20:53

1 Answer 1

1

I saw your full source code and you have a bunch of 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.

Change your query to SELECT <LIST> ... where LIST is a comma separated list of column names from which you want to retrieve values.

Sign up to request clarification or add additional context in comments.

6 Comments

There is no "correct" query, what is it you want your query to return?
I want the query to return the number of each component.
Then why are you creating a new DCDataObj(...) object after your query. You should just be creating an int as int count = result.getInt(1);
Yes but I want to fill the Java Object with the number of each component. With one SQL query I want to get the number of each component
Your class for that Object doesn't have a field for count. You're going to have to store the count differently.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.