2013-12-22 53 views
1

和查詢的情況下,我使用下面的查詢爲:如何使用NetBeans中

String qry1 = "SELECT SUM(*) FROM class_cse where Student_ID='" + StudentUserBean.getUserId() + "'"; 
    try { 
     ResultSet rs, rs1; 
     rs = db2.stmt.executeQuery(qry1); 
     while (rs.next()) { 
      String count = rs.getString("SUM(*)"); 
      jTextField6.setText(count); 
     } 
    } catch (SQLException ex) { 
     Logger.getLogger(AdminInfo.class.getName()).log(Level.SEVERE, null, ex); 
    } 

現在我用這個查詢

String qry1=select sum(case when maths = 'ABSENT' then 1 else 0 end) + 
      sum(case when ca = 'ABSENT' then 1 else 0 end) + 
      sum(case when cn = 'ABSENT' then 1 else 0 end) 
    from attendance_table; 

此查詢的總和jTextField5設置。你能告訴我如何使用這個查詢嗎?

+1

目前還不清楚你在問什麼。請澄清。 –

回答

0

你有兩種選擇。第一是由位置獲得的結果,而不是通過柱名:

String sum = rs.getString(1); 

或:

int sum = rs.getInt(1); 

第二是增加一個別名的結果,例如myresult:

String qry1=select sum(case when maths = 'ABSENT' then 1 else 0 end) + 
      sum(case when ca = 'ABSENT' then 1 else 0 end) + 
      sum(case when cn = 'ABSENT' then 1 else 0 end) myresult 
    from attendance_table; 

,並使用它:

String count = rs.getString("myresult"); 
+0

我的問題解決了..請告訴我'別名'在sql中的工作原理。 – user2693664

+0

只要您可以將任何名稱添加到select子句中的列中,然後在讀取列時就可以使用這些名稱。 –