2013-02-26 86 views
0

我有一個SQL查詢,我想用它來計算組件到表中。java.sql.SQLException:列索引無效

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查詢?

+1

您是否在'ps.setInt'或'result.getInt'處得到錯誤? – 2013-02-26 20:52:54

+0

完整的堆棧跟蹤應該顯示問題所在的行。 – beny23 2013-02-26 20:53:50

回答

1

我看到了您的完整源代碼,並且您有一堆result.getInt(INDEX)。由於您只做SELECT COUNT(1),因此只有一列,所以對於除1之外的任何值INDEXgetInt()都將失敗。

將您的查詢更改爲SELECT <LIST> ...,其中LIST是要從中檢索值的列名的逗號分隔列表。

+0

沒有「正確的」查詢,您希望查詢返回的是什麼? – 2013-02-26 21:03:45

+0

我希望查詢返回每個組件的編號。 – 2013-02-26 21:05:01

+0

那麼爲什麼你在查詢後創建一個'新的DCDataObj(...)'對象。您應該創建一個'int'作爲'int count = result.getInt(1);' – 2013-02-26 21:06:23

相關問題