连接代码:public class ConnectionFactory {
  private static final String DRIVER="com.mysql.jdbc.Driver";
  private static final String URL="jdbc:mysql://192.168.0.6:3306/pbx";
  private static final String USERNAME="root";
  private static final String PASSWORD="root";  public static Connection getConnectionFactory()
  {
    Connection con=null;
    try {
      Class.forName(DRIVER);
      con=DriverManager.getConnection(URL,USERNAME,PASSWORD);
    }
    catch (Exception ex) {
      System.out.println("数据库连接失败。");
      ex.printStackTrace();
    }
    return con;
  }
数据库操作代码:public class PbxDAO {
Connection conn=ConnectionFactory.getConnectionFactory();
String sql="";
//添加pbx告警信息
@SuppressWarnings("finally")
public boolean savePbx(PbxBean dto) throws SQLException
{      
sql="insert into t_pbx(begin_time,end_time,type,content) values(?,?,?,?)";
    PreparedStatement pst=null;
    int i=0;
    boolean flag=false;
    pst=conn.prepareStatement(sql);
    pst.setLong(1, dto.getBegin_time());
    pst.setLong(2, dto.getEnd_time());
    pst.setInt(3, dto.getType());
    pst.setString(4, dto.getContent());
    try {
      i=pst.executeUpdate();
      if(i>0)
      {
       flag=true;
      }else
      {
       flag=false;
      }
    }
    catch (SQLException ex) {
      flag=false;
    }finally{
     try {
pst.close();
conn.close();
} catch (final SQLException e) {
e.printStackTrace();
}
    
      return flag;
    }
  } //查询pbx告警信息
public List findPbx(String sql)
{
List list=new ArrayList();
return list;
}
}
错误信息:com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.checkClosed(Connection.java:1932)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4768)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4734)
at com.pbx.web.dao.PbxDAO.savePbx(PbxDAO.java:25)
at com.pbx.web.service.PbxService.savePbx(PbxService.java:15)
at com.pbx.web.servlet.LoadDataServlet.doGet(LoadDataServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)
高手给指点下,谢谢!

解决方案 »

  1.   

    No operations allowed after connection closed.
      

  2.   

    return flag;
     这句放到 finally外面去 看看行不
      

  3.   

     Connection conn=ConnectionFactory.getConnectionFactory();很少见到你这种写法:数据库连接的打开操作既不放在方法内部,也不放在构造函数中很危险的,既然是成员变量,说不准在其他哪个方法中就close了修改:这句话移到savePbx方法内部
      

  4.   

        Connection conn=ConnectionFactory.getConnectionFactory();
    写在方法savePbx里面
      

  5.   

    Connection conn=ConnectionFactory.getConnectionFactory();很少见到你这种写法:数据库连接的打开操作既不放在方法内部,也不放在构造函数中很危险的,既然是成员变量,说不准在其他哪个方法中就close了修改:这句话移到savePbx方法内部
      

  6.   

    你的Connection对象是全局的,是单例的。全局共享。我不清楚这里你有没有在其他地方有数据库操作。如果有,肯定会出错,因为,在一个操作中,connection关闭了,其他的也就关闭了。这建立在只实例化一次PbxDAO 对象,但多次调用数据库操作。的情况。
    例如:public class PbxDAO {
        Connection conn=ConnectionFactory.getConnectionFactory();
        String sql="";
        //添加pbx告警信息
        @SuppressWarnings("finally")
        public boolean savePbx(PbxBean dto) throws SQLException
        {      
            sql="insert into t_pbx(begin_time,end_time,type,content) values(?,?,?,?)";
            PreparedStatement pst=null;
            int i=0;
            boolean flag=false;
            pst=conn.prepareStatement(sql);
            pst.setLong(1, dto.getBegin_time());
            pst.setLong(2, dto.getEnd_time());
            pst.setInt(3, dto.getType());
            pst.setString(4, dto.getContent());
            try {
              i=pst.executeUpdate();
              if(i>0)
              {
                  flag=true;
              }else
              {
                  flag=false;
              }
            }
            catch (SQLException ex) {
              flag=false;
            }finally{
                try {
                    pst.close();
                    conn.close();
                } catch (final SQLException e) {
                    e.printStackTrace();
                }
                
              return flag;
            }
      }    //查询pbx告警信息
        public List findPbx(String sql)
        {
            List list=new ArrayList();
           //比如这里已经实现了对数据库的操作.
            return list;
        }
    }
    public class PbxService
    {
       public void PbxUsrService()
       {
          PbxDAO pbxDAO = new PbxDAO();
          //第一次调用了findPbx
          List resultList = pbxDAO.findPbx("select * from pbx");
          ........
          //第二次调用了savePbx
          pbxDAO.savePbx(....);   }
    }
    这种情况下就会出现问题,因为,建立PbxDAO对象的时候就初始化了Connection对象,由于Connection对象是单例全局的,所以在前一个调用中关闭了,所以影响到后一个调用中的状态。
      

  7.   

    可以修改为:把Connection对象的实例化放到具体函数的内部。
    这样写应该就可以了public class PbxDAO {
        Connection conn=null;
        String sql="";
        //添加pbx告警信息
        @SuppressWarnings("finally")
        public boolean savePbx(PbxBean dto) throws SQLException
        { 
            conn = ConnectionFactory.getConnectionFactory();
            sql="insert into t_pbx(begin_time,end_time,type,content) values(?,?,?,?)";
            PreparedStatement pst=null;
            int i=0;
            boolean flag=false;
            pst=conn.prepareStatement(sql);
            pst.setLong(1, dto.getBegin_time());
            pst.setLong(2, dto.getEnd_time());
            pst.setInt(3, dto.getType());
            pst.setString(4, dto.getContent());
            try {
              i=pst.executeUpdate();
              if(i>0)
              {
                  flag=true;
              }else
              {
                  flag=false;
              }
            }
            catch (SQLException ex) {
              flag=false;
            }finally{
                try {
                    pst.close();
                    conn.close();
                } catch (final SQLException e) {
                    e.printStackTrace();
                }
                
              return flag;
            }
      }    //查询pbx告警信息
        public List findPbx(String sql)
        {
            List list=new ArrayList();
           //比如这里已经实现了对数据库的操作.
            return list;
        }
    }
      

  8.   

    同意5楼的说法,当你打开连接后,在一段时间内(很短的时间)不使用,这个连接是不会永远为你打开的,而它在你使用之前就已经关闭了。所以Connection conn写在方法savePbx里面就可以。
      

  9.   

    Connection conn=ConnectionFactory.getConnectionFactory();很少见到你这种写法:数据库连接的打开操作既不放在方法内部,也不放在构造函数中很危险的,既然是成员变量,说不准在其他哪个方法中就close了修改:这句话移到savePbx方法内部
      

  10.   

    谢谢楼上诸位的回答,半年没写java了,让朋友传多来个例子,照搬过来给悲剧了,唉,学艺不精啊!