日期:2014-05-17  浏览次数:20735 次

一个数据库很棘手的问题,高手请进

String sql = "select id,title,content,createdtime from blog where id="
+ id;

QueryRunner qr = new QueryRunner(ds);
List list =(List) qr.query(sql, new BeanListHandler<Blog>(Blog.class));

Blog blog = (Blog)list.get(0);

如果不用QueryRunner方法,单纯的写数据库代码,怎么放结果集放入List中 而且还是Blog.class类型

------解决方案--------------------
探讨
String sql = "select id,title,content,createdtime from blog where id="
+ id;

QueryRunner qr = new QueryRunner(ds);
List list =(List) qr.query(sql, new BeanListHandler<Blog>(Blog.class));

Blog ……

------解决方案--------------------
Java code

 public TabCntvsOrder findObject(String ordernumber)
        {
            DbConnection dbcon = null;
            Connection con=null;
            PreparedStatement ps=null;
            Statement st=null;
            ResultSet rs=null;
            ObjectClass oc= new ObjectClass();
                try {
                    dbcon = new DbConnection();
                    con =dbcon.getCon();
                    st = con.createStatement();
                    String sql = "select * from TAB_CNTVSORDER where ORDERNUMBER='"+ordernumber+"'";
                    rs = st.executeQuery(sql);
                    while(rs.next()){
//将循环拿出的结果放入对象中去
                    oc.setId(rs.getInt(1));

                                    }
                                  
                   } catch (Exception e) {
                    e.printStackTrace();
                } 
                finally {
                    try {if (ps!=null) ps.close();}
                    catch (Exception e) {}
                    try {if (con!=null) con.close();}
                    catch (Exception e) {}
                 
            }     
                return tco;//返回你设值的对象
        }