我有一个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),
...............
以下是完整的源代码:http://pastebin.com/YMvqBPpV
我收到以下错误消息:java.sql.SQLException:列索引无效
这个设计问题或问题是否在SQL查询中?
答案 0 :(得分:1)
我看到了您的完整源代码,并且您有一堆result.getInt(INDEX)
。由于您只执行SELECT COUNT(1)
,因此只有一列,因此对于除{1}之外的任何INDEX
值,getInt()
都将失败。
将您的查询更改为SELECT <LIST> ...
,其中LIST
是一个以逗号分隔的列名列表,您可以从中检索值。