##################################数据库连接操作javabean:
public class DbTrans {  public static Connection conn;
  Statement stmt;  /**
   * 构造函数
   */
  public DbTrans(){
      initConnection();
  }  /**
   * 带参数的构造函数
   * @@param conn 连接
   */
  public DbTrans(Connection conn){
      this.conn = conn;
  }  /**
   * 初始化建立连接
   */
  private void initConnection(){
      try{
          if(conn == null||conn.isClosed()){
//              DBConnectionManager connMgr=DBConnectionManager.getInstance();
//              conn = connMgr.getConnection("sqlconn");
            conn=DBConnectionManager.getConnection();
          }
      }
      catch(Exception ex){
          System.out.println("Can not get new Connection"+ex.getMessage());
      }
  }  public PreparedStatement getPreparedStmt(String sql) throws SQLException{
      PreparedStatement preStmt=null;
      try {
          preStmt = conn.prepareStatement(sql);
      }
      catch(SQLException ex){
          ex.printStackTrace();
          throw ex;
      }
      return preStmt;
  }
  /**
   * executeQuery操作,用于数据查询,主要是Select
   * @@param sql 查询字段
   * @@return 数据集
   * @@throws SQLException 捕捉错误
   */
  public ResultSet executeQuery(String sql) throws SQLException {
      ResultSet rs = null;
      try {
       stmt = conn.createStatement();
       rs = stmt.executeQuery(sql);
      }
      catch (SQLException ex) {
          ex.printStackTrace();
          System.out.println("dbTrans.executeQuery:"+ex.getMessage());
          throw ex;
      }
      return rs;
  }  /**
   * executeUpdate操作,用于数据更新,主要是Update,Insert
   * @@param sql 查询字段
   * @@throws SQLException 捕捉错误
   */
  public void executeUpdate(String sql) throws SQLException {
    try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        stmt.executeUpdate(sql);
    }
    catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("dbTrans.executeUpdate:"+ex.getMessage());
        throw ex;
    }
  }  public int[] doBatch(String sql) throws SQLException {
      int[] rowResult=null;
      String a;
      try{
          stmt = conn.createStatement();
          StringTokenizer st=new StringTokenizer(sql,";");
          while (st.hasMoreElements()) {
              a = st.nextToken();
              stmt.addBatch(a);
          }
          rowResult=stmt.executeBatch();
      }
      catch (SQLException ex) {
          ex.printStackTrace();
          System.out.println("dbTrans.doBatch"+ex.getMessage());
          throw ex;
      }
      return rowResult;
  }  /**
   * 关闭对象
   * @@throws SQLException 捕捉错误
   */
  public void close() throws SQLException{
    if(stmt != null) stmt.close();
    if(conn != null&&!conn.isClosed()) conn.close();
  }
}##################################javabean:
public class Post {  String SQL = "";
  int PostID;
  DbTrans DBSQL;
  ResultSet rs = null;
  public Post() {
      this.DBSQL = new DbTrans();
  }
    public String getBadWords(){
        String BadWord="";
        try {
          SQL = "select * from badwords";
          ResultSet rs = DBSQL.executeQuery(SQL);
            while(rs.next()){
              BadWord+=rs.getString("BadWord");
            }
            rs.close();
        }
        catch (SQLException e) {
        }
    return BadWord;
      }
  public void close() {
      try {
          DBSQL.close();
      }
      catch (SQLException e) {
      }
  }
}#########################jsp程序:
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<jsp:useBean id="PostBean" scope="page" class="com......" /><%
out.print(PostBean.getBadWords());
%>
以上jsp程序用完后,怎么释放该数据库连接?
是不是在jsp程序的最后加入PostBean.close();
???

解决方案 »

  1.   

    这个问题只要你注意点,使用完后记得关闭。
    你说的:
    以上jsp程序用完后,怎么释放该数据库连接?
    是不是在jsp程序的最后加入PostBean.close();
    这个问题,我觉得应该这样,数据库的释放应该在javabean或者ejb里面进行,最好不要在jsp页面进行。只要在你数据库操作完后关闭数据库连接就可以了,我们一般是这样操作的。
    try
    {
        数据库操作
    }
    catch(SQLException e)
    {
        异常处理
    }
    finally
    {
              closeDB(conn,pst,rs);
    } public static void closeDB(Connection conn,Statement stmt) throws SystemException
      {
        try {
            if(stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            SystemException lmsse = new SystemException(e);
            lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
            throw lmsse;
        }
      }
      public static void closeDB(Connection conn,Statement stmt,ResultSet rs) throws SystemException
      {
        try {
            if(rs !=null) rs.close();
            if(stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            SystemException lmsse = new SystemException(e);
            lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
            throw lmsse;
        }
      }
      

  2.   

    public static void closeDB(Connection conn,Statement stmt,ResultSet rs) throws SystemException
      {
        try {
            if(rs !=null) rs.close();
            if(stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            SystemException lmsse = new SystemException(e);
            lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
            throw lmsse;
        }
      }
      

  3.   

    其实很简单,不要看代码看胡涂了。
    只要在每次你从连接此获得connection之后,在进行完操作的时候记得conn。close()就是。当然在这之前还把其他的statement和resultset关闭
      

  4.   

    to hxzhappy(冰雨),把你的连接池代码给我看一下,谢谢。
      

  5.   

    以上各位,conn.close(),是关闭链接还是释放连接给连接池???连接池代码里体现在哪?
      

  6.   

    一般数据库连接池,用完就close的
    这就算释放了,conn关闭连接,连接池也就知道该conn是free的good luck
      

  7.   

    一般来说,对于由weblogic等中间件提供的连接池在connection用完close以后,会自动将该connection进行垃圾回收,即会在连接此中释放该connection。
    这一步是不用热外编程的。但是如果连接池的代码是自己写的话,仅仅close是不够的。还必须将你对应的放置connection的useConnections这个hashtable里面的该conn进行remove。
      

  8.   

    to conning333(chen),主帖里已经有连接池代码,请写一下代码,需要调用哪个方法才能将连接返回给连接池?
      

  9.   

    现在一般的weblogic.tomcat都提供了内置的连接池程序。你只要配置好jndi就可以用了,不需要在自己来写连接池的代码了,我有详细的配置文挡。需要的话请上MSN来找我[email protected]