我的想法是这两种思路中,从java的特性分析,或者程序性能上分析,哪个更好

解决方案 »

  1.   

    我觉得在你的b方法中try一下!在finally得时候关闭连接!
      

  2.   

    我的一个类中的方法:
    public void dataActionSelect(){
    Data db=null;
    String sql="select id,senday,topic,msg from msg where name=? and toname=? order by senday desc";
    try{
    db=new Data(sql);
    db.setString(1,Msg_name);
    db.setString(2,Msg_toname);
    db.executeQuery();
    if(db.next()){
    Msg_id=db.getInt(1);
    Msg_senday=db.getString(2);
    if(Msg_senday!=null && !Msg_senday.equals("")){
    Msg_senday=Msg_senday.substring(0,10);
    }
    Msg_topic=db.getString(3).trim();
    Msg_msg=db.getString(4);
    }else{
    errmsg="没找到";
    }
    }catch(Exception e){
    errmsg="查询操作出错";
    System.out.print("Selecting error:"+e.getMessage());
    }finally{
    try{
    if(db!=null){
    db.close();
    }
    }catch(Exception e){
    errmsg="关闭数据库出错";
    System.out.print("Closing error:"+e.getMessage());
    }
    }
    }
      

  3.   

    如上是我的一个完整的方法,应用链接池技术,这样使用我认为是合理的,我的很多方法都与此类似,尽管每个方法都新创建了Data对象实例,但使用完毕的时候都释放掉了.想听大伙更多的意见
      

  4.   

    补充下,我定义了一个接口类,其中定义的四个方法分别是
    public void DataActionSelect();
    public void DataActionUpdate();
    public void DataActionInsert();
    public void DataActionDelete();
    类A就是实现该接口的一个类