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?
COMPONENT_TYPEandCNT. Are you trying to retrieve the value for one of those columns from the first row of the result and pass that to yourDCDataObjconstructor? If so, are you trying to retrieve theCNT? Or theCOMPONENT_TYPE? My guess would be theCNT.CNT.