连接池

package webbook.util;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

public class ConnectionPool {

     private Vector<Connection> pool;

     private String url;

     private String username;

     private String password;

     private String driverClassName;

     /**
  * 连接池的大小,也就是连接池中有多少个数据库连接。
  */
     private int poolSize = 1;

     private static ConnectionPool instance = null;

     /**
  * 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法。
  * 使用了设计模式中的单子模式。
  */
     private ConnectionPool() {
  init();
    }

     /**
  * 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连接
  */
     private void init() {
  pool = new Vector<Connection>(poolSize);
  readConfig();
  addConnection();
    }

     /**
  * 返回连接到连接池中
  */
     public synchronized void release(Connection conn) {
  pool.add(conn);

    }

     /**
  * 关闭连接池中的所有数据库连接
  */
     public synchronized void closePool() {
   for ( int i = 0; i < pool.size(); i++) {
       try {
    ((Connection) pool.get(i)).close();
      } catch (SQLException e) {
    e.printStackTrace();
      }
      pool.remove(i);
  }
    }

     /**
  * 返回当前连接池的一个对象
  */
     public static ConnectionPool getInstance() {
   if (instance == null) {
      instance = new ConnectionPool();
  }
   return instance;
    }

     /**
  * 返回连接池中的一个数据库连接
  */
     public synchronized Connection getConnection() {  
   if (pool.size() > 0) {
      Connection conn = pool.get(0);
      pool.remove(conn);
       return conn;
  } else {
       return null;
  }
    }

     /**
  * 在连接池中创建初始设置的的数据库连接
  */
     private void addConnection() {
  Connection conn = null;
   for ( int i = 0; i < poolSize; i++) {

       try {
    Class.forName(driverClassName);
    conn = java.sql.DriverManager.getConnection(url, username, password);
    pool.add(conn);

      } catch (ClassNotFoundException e) {
    e.printStackTrace();
      } catch (SQLException e) {
    e.printStackTrace();
      }

  }
    }

     /**
  * 读取设置连接池的属性文件
  */
     private void readConfig() {
   try {
      String path = System.getProperty( "user.dir") + "\\dbpool.properties";
      FileInputStream is = new FileInputStream(path);
      Properties props = new Properties();
      props.load(is);
       this.driverClassName = props.getProperty( "driverClassName");
       this.username = props.getProperty( "username");
       this.password = props.getProperty( "password");
       this.url = props.getProperty( "url");
       this.poolSize = Integer.parseInt(props.getProperty( "poolSize"));
  } catch (Exception e) {
      e.printStackTrace();
      System.err.println( "读取属性文件出错. ");  
  }
    }
}
 
<Resource name="jdbc/oracleds" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:oracle" />
 
 Context context = new InitialContext();
    DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/oracleds");
    conn = ds.getConnection();
 
public class StringUtil {

     /**
  * 判断输入的字符串参数是否为空。
  * @param args 输入的字串
  * @return true/false
  */
     public static boolean validateNull(String args) {
   if (args == null || args.length() == 0) {
       return true;
  } else {
       return false;
  }
    }

     /**
  * 判断输入的字符串参数是否为空或者是"null"字符,如果是,就返回target参数,如果不是,就返回source参数。
  */
     public static String chanageNull(String source, String target) {
   if (source == null || source.length() == 0 || source.equalsIgnoreCase( "null")) {
       return target;
  } else {
       return source;
  }
    }

     /**
  * 过滤<, >,\n 字符的方法。
  * @param input 需要过滤的字符
  * @return 完成过滤以后的字符串
  */
     public static String filterHtml(String input) {
   if (input == null) {
       return null;
  }
   if (input.length() == 0) {
       return input;
  }
  input = input.replaceAll( "&", "&");
  input = input.replaceAll( "<", "<");
  input = input.replaceAll( ">", ">");
  input = input.replaceAll( " ", " ");
  input = input.replaceAll( "'", "'");
  input = input.replaceAll( "\"", """);
   return input.replaceAll( "\n", "<br>");
    }
}
 
 

你可能感兴趣的:(开发,职场,休闲)