急。问题简单。进来分皆有分。回答者分多。不管对还是错
String hql= "select count(*) sum,eventStateID from t_Event event group by eventStateID ";
org.hibernate.Session s = org.trundle.db.HibernateUtils.currentSession();
java.util.List eventList=s.createSQLQuery(hql).list();
if (eventList != null && eventList.size() != 0) {
Iterator it = eventList.iterator();
while(it.hasNext())
{
。。。。。
}
}
问题:
从数据库查出来的sum,跟eventStateID 值,有很多,但都对应的,我想从eventList表中取出各个sum,跟eventStateID 值应该怎么做?程序怎么写?
------解决方案--------------------eventList里面保存的是结构体吧,循环从结构体里面取?
------解决方案--------------------接分喽
------解决方案--------------------public ArrayList query(String sbid) {
ArrayList objAL = new ArrayList();
int subUserID = Integer.parseInt(sbid);
try {
con = objConnManager.getConnection();
pstmt = con.prepareStatement(this.getStrSql());
pstmt.setInt(1, subUserID);
rs = pstmt.executeQuery();
while (rs.next()) {
CarInfoBean cb = new CarInfoBean();
cb.setCarID(rs.getInt(1));
cb.setCarNum(rs.getString(2));
cb.setDriverName(rs.getString(3));
cb.setSimNum(rs.getString(4));
cb.setCarType(rs.getString(5));
cb.setCarColor(rs.getString(6));
cb.setCarBrand(rs.getString(7));
cb.setCarNo(rs.getString(8));
cb.setRemark(rs.getString(9));
objAL.add(cb);
}
}
catch (
SQLException ex) {
System.out.println( "数据库操作失败! " + ex.getMessage());
}
finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
}
catch (SQLException sqlex) {
System.out.println( "关闭数据库
连接失败! " + sqlex.getMessage());
}
}
return objAL;
}
servlet里面用ArrayList接受
jsp里面 通过for循环把每个javaBean对象取出来
------解决方案--------------------楼主用的是hibernate吧
Iterator it = eventList.iterator();
while(it.hasNext()){
Object[] obj = (Object[]).next();
obj[0].toString;//sum的值
obj[1].toString;//eventStateID的值
}
------解决方案--------------------request.setAttribute( "eventList ", eventList);
然后页面中循环得到就行
------解决方案--------------------潜水多年,jf先
------解决方案--------------------select t.eventStateID,count(t.eventStateID) as sum
from t_Event t
group by t.eventStateID
------解决方案--------------------select count(*) sum,eventStateID from t_Event event group by eventStateID;
换成
select eventStateID,count(*) sum from t_Event event group by eventStateID;
试试,不确定