天天看点

数据库关闭连接

    try{

    if (conn != null && stm != null && res != null) {

      conn.close();

      stm.close();

      res.close();

    }

    }catch(Exception e){

      e.printStackTrace();

      System.out.println("关闭数据库连接出错!");

上述代码会导致报错,因为stm关闭时res也同时关闭,执行res.close()时res已经关闭,所以导致报错。

以下代码为正确。

                try {

                        if (res != null)

                                res.close();

                } catch (Exception e) {

                        e.printStackTrace();

                }

                        if (stm != null)

                                stm.close();

                        if (conn != null) {

                          conn.close();

                        }