哪位大侠能提供一个比较好的操作连接池的类?考虑到资源释放等因素。

解决方案 »

  1.   

    瞎写的,不知道对不对。
    DataSource ds = null;
    Connection conn = null;
    Statement stat = null;
    ResultSet rst = null;
    try {
      DataSource ds = ctx.lookup("name");
      conn = ds.getConnection();
      //这里操作statement
      rst.close();
      statement.close();
      rst.close();  
    } catch (SQLException e) {
      try {
        rst.close();
        rst = null;
      } finally {
        if (null != rst) {
           rst.close();
        }
      }
      try {
        stat.close();
        stat = null;
      } finally {
        if (null != stat) {
           stat.close();
        }
      }
        try {
        conn.close();
        conn = null;
      } finally {
        if (null != rst) {
           conn.close();
        }
      }
    }接分
      

  2.   


    import java.sql.*;import javax.naming.*;
    import javax.sql.*;public class CreateDBO {    private static InitialContext initialcontext = null;    private static DataSource datasource = null;    public CreateDBO() {
        }    public static Connection getCenterConnection(boolean autocommit) {
            Connection connection = null;
            try {
                if (initialcontext == null)
                    initialcontext = new InitialContext();
                if (datasource == null) { /**
     * 相关参数在.xml文件里面设置
     */
                    datasource = (DataSource) initialcontext
                            .lookup("java:/datasource");
                    if (datasource == null)
                        datasource = (DataSource) initialcontext
                                .lookup("java:comp/env/jdbc/datacenter");
                }
                connection = datasource.getConnection();
                connection.setAutoCommit(false);        } catch (Exception e) {
                e.printStackTrace();
            }
            return connection;
        }    public static Connection getCenterConnection() {
            Connection connection = getCenterConnection(false);
            return connection;
        }    public static PreparedStatement getPreparedStatement(Connection conn,
                String StatementString) {
            PreparedStatement preparedstatement = null;
            try {
                preparedstatement = conn.prepareStatement(StatementString);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return preparedstatement;
        }    public  static Statement getStatement(Connection conn) {
            Statement statement = null;
            try {
                statement = conn.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return statement;
        }    public  void Commit(Connection conn) {
            try {
                conn.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public  void Rollback(Connection conn) {
            try {
                conn.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public  void CloseConnection(Connection connection, boolean isSuccess) {
            try {
                // Rollback or commit the transaction            try {
                    if (isSuccess == false) {
                        connection.rollback();
                    } else {
                        if (connection != null) {
                            if (!connection.getAutoCommit()) {
                                try {
                                    connection.commit();
                                } catch (Exception e) {
                                    connection.rollback();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }        } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                        connection = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }    }    public  void CloseConnection(Connection connection) {
            try {
                //  commit the transaction            try {                if (connection != null) {
                        if (!connection.getAutoCommit()) {
                            try {
                                connection.commit();
                            } catch (Exception e) {
                                connection.rollback();
                            }
                        }
                    }             } catch (Exception e) {
                    e.printStackTrace();
                }        } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null && !connection.isClosed()) {
                        connection.close();
                        connection = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }    }    public  void CloseStatement(Statement stmt) {
            try {
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public  void CloseResultSet(ResultSet rst) {
            try {
                if (rst != null) {
                    rst.close();
                    rst = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}