少年阿宾

那些青春的岁月

  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.LinkedList;
import java.util.List;

public class ConnectionManage {
 private static List<Connection> pool=new LinkedList<Connection>();;
 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 static int initCount = 5;
 private static int maxCount = 10;
 private static int currentCount = 5;
 private static volatile ConnectionManage instance = null;
 private ConnectionManage(){
  init();
 }
 public static ConnectionManage getInstance(){
  if(null == instance){
   synchronized (ConnectionManage.class) {
    if(null == instance){
     return new ConnectionManage();
    }
   }
  }
  return instance;
 }
 public void init(){
  addConnection();
 }
 public static Connection createConnection() throws ClassNotFoundException {
  Connection conn=null;
  try {
   Class.forName(DRIVER);
   conn=DriverManager.getConnection(Url, USERNAME, PASSWORD);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }
 public static void addConnection(){
  for(int i=0;i<initCount;i++){
   try {
    pool.add(createConnection());
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void release(Connection conn){
  pool.add(conn);
 }
 public static Connection getConnection() throws SQLException, ClassNotFoundException{
  synchronized (pool) {
   if(pool.size()>0){
    System.out.println("Current Connection size is:"+pool.size());
    return pool.get(0);
   }else if(currentCount<maxCount){
    Class.forName(DRIVER);
    Connection conn=createConnection();
    pool.add(conn);
    currentCount++;
    return conn;
   }else{
    throw new SQLException("Current Connection is Zero");
   }
  }
 }

}





测试代码:

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=ConnectionManage.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:18 abin 阅读(1542) 评论(0)  编辑  收藏 所属分类: JDBCP

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


网站导航: