我有这个 Java 对象,我将使用它来存储表中的计数元素:
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;
}
..........
}
我使用此 SQL 查询将组件计数到 Oracle 表中:
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);
......
我使用以下 Java 代码得到结果:
ResultSet result = ps.executeQuery();
while (result.next())
{
dc = new DCDataObj(
result.getInt(1),
result.getInt(2),
result.getInt(3),
...........
);
}
执行查询时出现此问题:
java.sql.SQLException: Invalid column index
你能帮我解决这个问题吗?
更新
The SQL query works. I get this result:
CNT
----------------------
1
1
1 1
我怀疑问题出在返回类型上。我想我得到的结果是数组。但是我可以在不使用数组的情况下以某种方式将查询结果插入到 Java 对象中吗?