connpool.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 267 行

JAVA
267
字号
package cn.js.fan.db;import java.sql.*;import javax.naming.Context;import javax.sql.DataSource;import javax.naming.InitialContext;import javax.naming.NamingException;import cn.js.fan.web.Global;public class ConnPool {    boolean debug = true;  java.sql.Connection con = null;  Statement stmt = null;  ResultSet rs = null;  ResultSetMetaData resultsMeta = null;  int rows = 0;  int columns = 0;  String PoolName = null;  boolean inited = false;    public ConnPool() {  }  public void init() {    try {      Context initCtx = new InitialContext();      Context envCtx = (Context) initCtx.lookup("java:comp/env");      DataSource ds = (DataSource) envCtx.lookup("jdbc/"+PoolName);      con = ds.getConnection();      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                                 ResultSet.CONCUR_READ_ONLY);    }        catch (NamingException e) {      System.out.println("Connection pool fail. Message is:");      System.out.println(e.getMessage());    }    catch (SQLException e) {      System.out.print("SQL Exception occur. Message is:");      System.out.print(e.getMessage());    }    inited = true;  }  public ConnPool(String pn) {    try {      Context initCtx = new InitialContext();      Context envCtx = (Context) initCtx.lookup("java:comp/env");      DataSource ds = (DataSource) envCtx.lookup("jdbc/"+pn);      con = ds.getConnection();      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                                 ResultSet.CONCUR_READ_ONLY);    }        catch (NamingException e) {      System.out.println("Connection pool fail. Message is:");      System.out.println(e.getMessage());    }    catch (SQLException e) {      System.out.print("SQL Exception occur. Message is:");      System.out.print(e.getMessage());    }    PoolName = pn;    inited = true;  }  public void setPOOLNAME(String pn) {    if (!inited)    {      this.PoolName = pn;      init();    }  }  public String getPOOLNAME() {    return this.PoolName;  }    public ResultSet executeQuery(String sql) throws SQLException {    if (con == null)      return null;    ResultSet rs = null;    try {      rs = stmt.executeQuery(sql);    }    catch (SQLException e) {      System.out.print("Query:" + sql+"---"+ e.getMessage());      throw e;    }    this.rs = rs;    return rs;  }    public int executeUpdate(String sql) throws SQLException {    int rowcount = 0;    if (con == null)      return 0;    try {      rowcount = stmt.executeUpdate(sql);    }    catch (SQLException e) {      if (debug) {        System.out.println("Update:" + e.getMessage());        System.out.println("sql:" + sql);      }      throw e;    }    finally {      return rowcount;    }  }    public int getColumns() {    int columns = 0;    try {      this.resultsMeta = this.rs.getMetaData();      columns = this.resultsMeta.getColumnCount();    }    catch (SQLException e) {}    return columns;  }    public int getRows() {    rows = 0;    try {            rs.last();      rows = rs.getRow();      rs.beforeFirst();    }    catch (SQLException e) {      System.out.println("getRows error:" + e.getMessage());    }    return this.rows;  }  protected void finalize() throws Throwable {    super.finalize();    try {      if (stmt != null) {        stmt.close();        stmt = null;      }    }    catch (SQLException e) {      System.out.println("Conn finalize: "+e.getMessage());    }    try {      if (con != null && !con.isClosed())      {        con.close();        con = null;      }    }    catch (SQLException e) {      System.out.println("Conn finalize: "+e.getMessage());    }  }  public void close() {     try {      finalize();    }    catch (java.lang.Throwable e) {      System.out.println("conn.close error:" + e.getMessage());    }  }  public void beginTrans() throws SQLException {      if (!Global.isTransactionSupported)          return;      try {                    con.setAutoCommit(false);      } catch (SQLException ex) {          ex.printStackTrace();          System.out.print("beginTrans Errors");          throw ex;      }  }  public void commit() throws SQLException {      if (!Global.isTransactionSupported)          return;      try {          con.commit();          con.setAutoCommit(true);      } catch (SQLException ex) {          ex.printStackTrace();          System.out.print("Commit Errors");          throw ex;      }  }  public void rollback() {      if (!Global.isTransactionSupported)          return;      try {          con.rollback();          con.setAutoCommit(true);      } catch (SQLException ex) {          ex.printStackTrace();          System.out.print("Rollback Errors");                }  }  public boolean getAutoCommit() throws SQLException {    boolean result = false;    try {      result = con.getAutoCommit();    }    catch (SQLException ex) {      ex.printStackTrace();      System.out.println("getAutoCommit fail" + ex.getMessage());      throw ex;    }    return result;  }  public int getTransactionIsolation() throws SQLException {    int re = 0;    try {      re = con.getTransactionIsolation();    }    catch (SQLException e) {      e.printStackTrace();      System.out.println("getTransactionIsolation fail" + e.getMessage());    }    return re;  }  public void setFetchSize(int s) {    try {      stmt.setFetchSize(s);    }    catch (SQLException e) {      System.out.println("setFetchSize fail:" + e.getMessage());    }  }     public void setMaxRows(int maxRows) throws       SQLException {     try {       stmt.setMaxRows(maxRows);     }     catch (Throwable t) {                                          System.out.println("conn.setMaxRows:"+t.getMessage());     }   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?