鏈接池 ----單例模式

//數據庫鏈接池  單例模式  

import java.sql.Connection;  

import java.sql.SQLException;  

import com.mchange.v2.c3p0.ComboPooledDataSource;  

import com.mchange.v2.c3p0.DataSources;  

public class ComboPooledDataSourceDemo 
{
        private static ComboPooledDataSourceDemo instance;  

        private ComboPooledDataSource ds;  

        private ComboPooledDataSourceDemo() throws Exception
        {  
            ds = new ComboPooledDataSource();  

            ds.setDriverClass("oracle.jdbc.driver.OracleDriver");  //驅動

            ds.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");  //地址

            ds.setUser("test0816");  //數據庫用戶名

            ds.setPassword("934617699");  //數據庫用戶密碼

            //初始化時獲取三個鏈接,取值應在minPoolSize與maxPoolSize之間。Default: 5 initialPoolSize  

            ds.setInitialPoolSize(5);  

            //鏈接池中保留的最大鏈接數。Default:  20 maxPoolSize  

            ds.setMaxPoolSize(20);  

            //// 鏈接池中保留的最小鏈接數。  

            //ds.setMinPoolSize(1);  

            //當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 5 acquireIncrement    

            ds.setAcquireIncrement(10);  
     //用來返回該對象
        public  static  final ComboPooledDataSourceDemo getInstance() {  

            if (instance == null) {  

                try {  

                    instance = new ComboPooledDataSourceDemo();  

                } catch (Exception e) {  

                    e.printStackTrace();  

                }  

            }  

            return instance;  

        }  

      
            //返回一個鏈接
        public synchronized   final Connection getConnection() {  

            try {  

                return ds.getConnection();  

            } catch (SQLException e) {  

                e.printStackTrace();  

            }  

            return null;  

        }  
}
package com.phone.shuyinghengxie;

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

import org.junit.Test;

public class ComboPooledDataSourceDemoTest1 {

    public static void main(String[] args)
    {
        ComboPooledDataSourceDemo  cp = ComboPooledDataSourceDemo.getInstance() ;
        
        
            List<Person> list = new ArrayList<>() ;
            
            
            
            cp.getConnection() ;
            
            String sql = "select * from person " ;
            
            try {
                PreparedStatement  pt = cp.getConnection().prepareStatement(sql) ;
                
                ResultSet rt = pt.executeQuery() ;
                
                while(rt.next())
                {
                    Person p = new Person();
                    
                    p.setName(rt.getString("name"));
                    p.setAge(rt.getString("age"));
                    p.setSex(rt.getString("sex"));
                    p.setTelNum(rt.getString("telNum"));
                    p.setAddress(rt.getString("address"));
                    
                    list.add(p) ;
                            
                }
                
                for(Person p : list)
                {
                    System.out.println(p);
                }
                
                System.out.println("哈哈");
            } catch (SQLException e) {
                // TODO 自動生成的 catch 塊
                e.printStackTrace();
            }
        }


    }

}
相關文章
相關標籤/搜索