不懂楼主的意思.
具体你的Sql语句与JDBC无关,你查询完,返回一个ResultSet对象.
这时你new一个你所要返回的对象,比如:public class Test(){
private String bName;
private int sum;
get...set....methods
}
然后:
rs = stm.executeQuery(sql);//运行查询,并取结果集
List resultList =null;
if(rs != null){//判断结果集是否为空
resultList = new ArrayList();//new一个List
while(rs.next()){//循环取结果集,类似游标的作用
Test test = new Test();//你所要用来包装结果的对象
test.setBName(rs.getString("bname"));//从结果集中取出bname的值,类型为String
test.setSum(rs.getInt("sum"));//从结果集中取出sum的值,类型为int
resultList.add(test);//将对象保存到List中
}
}return resultList;//你要的结果对象List
具体你的Sql语句与JDBC无关,你查询完,返回一个ResultSet对象.
这时你new一个你所要返回的对象,比如:public class Test(){
private String bName;
private int sum;
get...set....methods
}
然后:
rs = stm.executeQuery(sql);//运行查询,并取结果集
List resultList =null;
if(rs != null){//判断结果集是否为空
resultList = new ArrayList();//new一个List
while(rs.next()){//循环取结果集,类似游标的作用
Test test = new Test();//你所要用来包装结果的对象
test.setBName(rs.getString("bname"));//从结果集中取出bname的值,类型为String
test.setSum(rs.getInt("sum"));//从结果集中取出sum的值,类型为int
resultList.add(test);//将对象保存到List中
}
}return resultList;//你要的结果对象List
解决方案 »
- 跪求!!!!啊~~<jsp:include page="1.jsp">
- java调用数据库,在JSP页面显示,可是jsp页面显示不出来,求帮助!谢谢
- 编写网页中的一个错误,请帮忙找出来
- 新手请教ResultSet数量
- 将Date类型的数据从数据库中读取并显示,有修改操作,但没有改动值,保存后出现类型转换错误
- 求一个URL重写正则表达式
- 弄了两天没弄出来,急等高人帮忙。在线等。struts checkbox问题
- 一个小小的调查,请大家配合下:)
- 怎样按指定的长度把内容输出在网页上?
- 求大神解决phantomJS 一个evaluate的常见问题
- 新建JSP群(15005766)诚邀各位加入~~~~~~~~~~
- 请教asp函数转用jsp实现
public Collection getDetailCount(String sql) throws Exception {
Collection colValue = new ArrayList();
try{
con = DbAccess.getInstance().getCon();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
while(rs.next()){
String typeName = rs.getString(1);
float accValue = rs.getFloat(2);
AccTypeCount atc = new AccTypeCount();
atc.setTypeName(typeName);
atc.setAccValue(accValue);
colValue.add(atc);
}
rs.close();
stm.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
return colValue;
}
就是把结果保存在Collection中,其中AccTypeCount是我为了保存结果而加的Bean,这个方法好吗,有没有更好的方法?
Collection colValue = new ArrayList();
try{
con = DbAccess.getInstance().getCon();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
while(rs.next()){
String typeName = rs.getString(1);
float accValue = rs.getFloat(2);
AccTypeCount atc = new AccTypeCount();
atc.setTypeName(typeName);
atc.setAccValue(accValue);
colValue.add(atc);
} }
catch(Exception e){
e.printStackTrace();
}finally{
try{
rs.close();
stm.close();
con.close();
}
catch(SQLException ex){}
}
return colValue;
}Usually,we put 'close' method in finally block. And ,rs perhaps could be null sometime.So I suggest that you should create another method to close your rs,stm and conn.