欢迎参观:http://netfish.aboutme.com/myclass3.htm我来帮你顶,UP!UP!

解决方案 »

  1.   

    if ( conn != null )
     conn.close();
      

  2.   

    Connection conn = //get database connection
    PreparedStatement psm = null;
    ResultSet rs = null;
    try {
      ...
    } catch (SQLException sqlx) {
      ...
    } finally {
      try {
        if (rs != null)
          rs.close();
        if (psm != null)
          psm.close();
        if (conn != null)
          conn.close();
      } catch (Exception ex) {
        System.out.println("can't close database connection : " + ex.toString());
      }
    }
      

  3.   

    package netgate;import java.io.*;
    import java.sql.*;public class conn {
      private static String userName="";
      private static String password="";
      private static String url="jdbc:mysql://localhost/trmg3?useUnicode=true&characterEncoding=gb2312";
      private Connection conn=null;
      private Statement stmt = null;
      private ResultSet rs=null;   public  conn()
      {
        try
         {
           netgate.DbConnPool.SetJDBC(url,userName,password);
           netgate.DbConnPool.setDriverName("com.mysql.jdbc.Driver");
           netgate.DbConnPool.setMinCount(10);
           conn = netgate.DbConnPool.getConnection();
         }catch(SQLException ex)
         {
             ex.printStackTrace(System.out);
             System.err.println("Can't connect to DataBase Use the Url:"+url+"\n"
                                +" Or UserName can't perform password!");     }
      }  public boolean executeInsert(String sql){  //写入数据库
        try{
          stmt=conn.createStatement();
          stmt.executeUpdate(sql);
          stmt.close();
          return true;
        }
        catch (SQLException ex){
          System.err.println("Update DataBase Error:"+ex.getMessage());
          return false;
        }
      }  public ResultSet executeQuery (String sql) throws SQLException{  //查询数据库
        if(rs != null)
          rs.close();
        try{
          stmt=conn.createStatement();
          rs=stmt.executeQuery(sql);
          return rs;    }
        catch(SQLException ex){
          System.err.println("Query DataBase Error:"+ex.getMessage());
          return null;
        }
      }  public  static  String  UnicodeToChinese(String  s){
        try{
          if(s==null || s.equals(""))  return  "";
          String  newstring=null;
          newstring=new  String(s.getBytes("ISO8859_1"),"gb2312");
          return  newstring;
        }
        catch(UnsupportedEncodingException  e)
        {
          return  s;
        }
      }  public  static  String  ChineseToUnicode(String  s){
        try{
          if(s==null || s.equals(""))  return  "";
          String  newstring=null;
          newstring=new  String(s.getBytes("gb2312"),"ISO8859_1");
          return  newstring;
        }
        catch(UnsupportedEncodingException  e)
        {
          return  s;
        }
      }  public void destory() throws Exception{
    try {
        if(rs != null)
         rs.close();
        if(stmt != null)
          stmt.close();
        if(conn != null)
          conn.close();
    }catch(Exception e){
      System.err.println("Close connection error:"+e);
        }
      }
    }
      

  4.   

    我已经在destory()中进行了判断了阿
      

  5.   

    看看,可能是你的ResultSet 中的rs游标指针为空,如果是select的查询,可以用executeQuery,如果是insert,update or delete,应该用executeUpdate(),虽然用executeQuery()方法也可以进行插入,删除和修改,但是就会出现你说的那个问题。不过这并不影响你的使用。
      

  6.   

    我的修改删除是通过executeInsert()来实现的,不是再executeQuery中实现的。
      

  7.   

    看看Connection的状态判断在哪一步为空的?
      

  8.   

    rs.close()时不是所有页面都有,是有些有,不知道原因,找找
      

  9.   

    象这种情况你debug一下不就行了吗,何苦让大家猜来猜去的?
      

  10.   

    是在rs.close的时候抛出的,我现在这么处理,大家看看是不是好:
      public void destory() throws Exception{
    try {
        if(rs != null)
        {
        rs.close();
      }
    }catch(Exception e){
    System.err.println("Close connection error:"+e);
      }try {
        if (stmt != null) {
          stmt.close();
        }
      }catch(Exception e){
    System.err.println("Close connection error:"+e);
      }try {
        if(conn != null)
        {
          conn.close();
        }
      }catch(Exception e){
    System.err.println("Close connection error:"+e);
    }
        finally{
        try {conn.close();}
        catch (Exception e) { e.printStackTrace(); }
    }  }
      

  11.   

    我这样处理后,原本关闭rs出错的地方还是会抛出错误,但是还会继续去执行后面的conn.close();另外还用finally来个保险!
      

  12.   

    try {
        if (rs != null) {
            rs.close();
            rs = null;
        }
        if (stmt != null) {
            stmt.close();
            stmt = null;
        }
        if (conn != null) {
            conn.close();
            conn = null;
        }
    } catch (Exceptio e) {
        if (rs != null) {
            System.err.println("Close resultset error:"+e);
        }
        if (stmt != null) {
            System.err.println("Close statement error:"+e);
        }
        if (conn != null) {
            System.err.println("Close connection error:"+e);
        }
    } finally {
      try {
          if (conn != null) {
              conn.close();
          }
      } catch (Exception ex) {
          System.err.println("Finally close connection error:"+e);
      }
    }个人认为这样的结构比较好一点,一个大try就可以了
      

  13.   

    上面的程序有点问题
    在catch(Exception e)部分
    不过只是提供一种思路,具体你可以再改过来
      

  14.   

    我多个try的原因是当rs.close()出错的时候还能继续执行下面的conn.close();象楼上那样的写rs.close出错的时候就退出了。
      

  15.   

    你的rs.close()之所以会出错
    原因在于如果没有调用executeQuery,rs就是空的
    或者调用executeQuery失败,rs也是空的
    rs.close()出错后
    可以在后面的finally里继续执行conn.close();
      

  16.   

    那倒是,finally中反正都要关闭conn。用多个try是不是不好?为什么?