少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConnectionPool {
 private List<Connection> pool;
 private static final String Url = "jdbc:oracle:thin:@localhost:1521:XE";
 private static final String USERNAME = "abing";
 private static final String PASSWORD = "abing";
 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
 private int poolSize = 10;
 private static volatile ConnectionPool instance = null;

 private ConnectionPool() {
  init();
 }

 public void init() {
  pool = new ArrayList<Connection>(poolSize);
  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 (null == instance) {
   synchronized (ConnectionPool.class) {
    if (null == instance) {
     return new ConnectionPool();
    }
   }
  }
  return instance;
 }

 public synchronized Connection getConnection() {
  if (pool.size() > 0) {
   Connection conn = pool.get(0);
   if(conn!=null){
    System.out.println("Connection Pool Create Success");
   }
   pool.remove(conn);
   return conn;
  }else{
   return null;
  }
 }

 public void addConnection() {
  Connection conn = null;
  for (int i = 0; i < poolSize; i++) {
   try {
    Class.forName(DRIVER);
    conn = DriverManager.getConnection(Url, USERNAME, PASSWORD);
    pool.add(conn);
   } catch (Exception e) {
    e.printStackTrace();
   }

  }
 }

}







测试代码:

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.abin.lee.collection.threadlocal.UserBean;

public class TestConnectionPool {

 public static List<UserBean> findAll(){
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  String sql="select * from userbean order by id desc";
  List<UserBean> list=new ArrayList<UserBean>();
  UserBean user=null;
  try {
   conn=ConnectionPool.getInstance().getConnection();
   ps=conn.prepareStatement(sql);
   rs=ps.executeQuery();
   while (rs.next()&&rs!=null) {
    user=new UserBean();
    user.setId(rs.getString("id"));
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
    list.add(user);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }
 public static void main(String[] args) {
  List<UserBean> list=findAll();
  for(UserBean lst:list){
   System.out.println("id="+lst.getId());
   System.out.println("username="+lst.getUsername());
   System.out.println("id="+lst.getPassword());
  }
 }


}

posted on 2012-09-04 00:20 abin 阅读(440) 评论(0)  编辑  收藏 所属分类: JDBCP

只有注册用户登录后才能发表评论。


网站导航: