try
{
}
catch(SQLException se)
{
stmt.close();
        conn.close();
stmt=null;
        conn=null;
 throw new Exception(se.getMessage());
}

解决方案 »

  1.   

    加一个try出现异常时直接在catch中关stmtstamt
      

  2.   

    因为我代码的风格是所有异常都是抛到最上一级处理。
    我现在想知道的是,通过throws抛出异常后,如何处理末关闭的连接。
      

  3.   

    try{}
    catch{异常}
    finally{关闭连接}  //(在finally给程序一个统一出口)
      

  4.   

    //增加如下语句
    finally
     {
       if (conn != null) {    
          try{ conn.close(); }
          catch(SQLException sqlex2){
              System.err("不能关闭数据库连接!");
                                     }
                         }
     }
      

  5.   

    whoami1618(火冰) 的是正解 !!!
      

  6.   

    struts中的事例:try-catch-finallytry {
    conn = dataSource.getConnection();
    stmt = conn.createStatement();
    rs =
    stmt.executeQuery("select * from employees where "
    + "username='" + username + "' "
    + "and password='" + password + "'");
    if ( rs.next() ) {
    user = rs.getString("username");
    // Iterate over the results
    System.err.println("Username : "
    + rs.getString("username")
    + " Password : " + rs.getString("password"));
    }
    else {
    System.err.println("&#8722;&#8722;&#8722;&#8722;>User not found<&#8722;&#8722;&#8722;&#8722;");
    }
    }
    catch (SQLException e) {
    System.err.println(e.getMessage());
    }
    finally {
    if (rs != null) {
    try {
    rs.close();
    }
    catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
    }
    rs = null;
    }
    if (stmt != null) {
    try {
    stmt.close();
    }
    catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
    }
    stmt = null;
    }
    if (conn != null) {
    try {
    conn.close();
    }
    catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
    }
    conn = null;
    }
    }
    return user;
    }
      

  7.   

    没有理解我的意思。
    例:
    public calss a{
         public a()
         throws SQLException{
           try{       
            b();
           }catch(Exception e){
             //这里对异常处理。
           }
        }
    }public class b(){
        private SDBAccess sdbaccess = new SDBAccess();//数据库工具
        public b()
          throws SQLException{
             connection conn = sdbaccess.getConnection(); //建立Connection
             Statement stm = conn.createStatement();
             //写一个错误的SQL语句,这样就会将错误抛向上一级。
             stm.executeQuery("当前是错误的SQL会出错误");
             stmt.close();
            conn.close();
          }
    }
    实际上,我在B中抛出的异常,是在A中处理,不用的东西也是在A中处理。但我不知道如何处理,
      

  8.   

    即使b抛出异常,
    它仍会继续执行b的 finally 
    !!!
    这就是java中finally关键字的用处.
    明白??
      

  9.   

    public calss a{
         public a()
         throws SQLException{
           try{       
            b();
           }catch(Exception e){
             //这里对异常处理。
           }finally{
              //处理其它情况。
           }
        }
    }是这样吗。。
    问题就在这,,我怎么关闭B中产生的Connection 和 Statement ,是否需要关闭。
      

  10.   

    通常我的class b 是用来做业务逻辑部份的,我不希望在b中处理如连接等错误,只在B中处理业务逻辑。class a 是我的控制类,在这里处理由业务产生的错误或异常,所以才有了上边的那种写法
      

  11.   

    class b中:
    try{
    }catch(
    關閉連接;
    throw new Exception(e.toString());

      

  12.   

    一般
    connection
    是传进去的,出来调用完了。
    一次COMMIT,这样多个业务方法就可以一起roolback
    也可以一起释放。
      

  13.   

    用连接池吧,数据库连接可是稀有资源啊,一般的连接池都支持timeout的,这样即使发生了Exception,连接没有关闭,连接池在一定时间后也会帮你强制关闭的.