简体   繁体   中英

Get result from Oracle SQL query

I have this Java Object which I will use to store counted elements from table:

private DCDataObj dc;

    public class DCDataObj
    {

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

        public DCDataObj()
        {
        }

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

        public int getDatacenter()
        {
            return datacenter;
        }

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

        ..........
    }

I use this SQL query to count the components into the Oracle table:

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);
......

I get the result using this Java code:

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

    dc = new DCDataObj(
            result.getInt(1),
            result.getInt(2),
            result.getInt(3),
            ...........
            );

}

I get this problem when I execute the query:

java.sql.SQLException: Invalid column index

Can you help me how I can solve this problem?

UPDATE

The SQL query works. I get this result:

    CNT                    
---------------------- 
1                      
1                      
1  1   

I suspect that the problem is into the return type. I suppose that I get the result as array. But can I somehow inset the result from the query into the Java object without using Array?

On each line, you end with a "?" but the next line starts with another "?" without a comma. Then you wind up with part of the string looking like ", ? ?," which is invalid JDBC syntax. You need commas in between all your "?" placeholders.

Try this, with commas added at the end of your lines "10", "20", and "30".

ps = conn.prepareStatement("SELECT CT.NAME AS COMPONENT_TYPE, 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");

EDIT

Now that I see your data, I see the problem. You cannot call getInt referencing the data, only the column header name or the 1-based column index. Also, your "COMPONENT_TYPE" is alphanumeric, please use getString instead of getInt . That also means you'll have to change your DCDataObj class to have a String for datacenter , not an int .

Try

dc = new DCDataObj(
   result.getString("COMPONENT_TYPE"),
   ...........
   );

or

dc = new DCDataObj(
   result.getString(1),
   ...........
   );

A colon in a bind variable or INTO specification was followed by an inappropriate name, perhaps a reserved word. You need to change the variable name and retry the operation. Did you try to get results from your query using pl/sql or SQL plus or your oracle terminal? Just to ensure you're executing the right query.

There is no column called "DATACENTER" fetched in the SELECT statement. It should be either COMPONENT_TYPE or CNT in result.getInt call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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