一个直接用java类实现连接池的代码例子

------------------------------config.java--------------------------------

package com.surge.pool;

/**
 * @author n
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

public class Config
{

    public static Config getInstance()
    {
        return instance;
    }

    private Config()
    {
        Digester digester = new Digester();
        digester.push(this);
        digester.addCallMethod("pl/database/driver", "setDatabaseDriver", 0);
        digester.addCallMethod("pl/database/url", "setDatabaseUrl", 0);
        digester.addCallMethod("pl/database/user", "setDatabaseUser", 0);
        digester.addCallMethod("pl/database/password", "setDatabasePassword", 0);
        InputStream is = null;
        try
        {
            is = getClass().getClassLoader().getResourceAsStream("db_config.xml");
            digester.parse(is);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch(SAXException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(is != null)
                    is.close();
            }
            catch(IOException exception) { }
        }
    }

    public String getDatabaseDriver()
    {
        return databaseDriver;
    }

    public void setDatabaseDriver(String databaseDriver)
    {
        this.databaseDriver = databaseDriver;
    }

    public String getDatabasePassword()
    {
        return databasePassword;
    }

    public void setDatabasePassword(String databasePassword)
    {
        this.databasePassword = databasePassword;
    }

    public String getDatabaseUrl()
    {
        return databaseUrl;
    }

    public void setDatabaseUrl(String databaseUrl)
    {
        this.databaseUrl = databaseUrl;
    }

    public String getDatabaseUser()
    {
        return databaseUser;
    }

    public void setDatabaseUser(String databaseUser)
    {
        this.databaseUser = databaseUser;
    }

    private String databaseDriver;
    private String databaseUrl;
    private String databaseUser;
    private String databasePassword;
    private static final Config instance = new Config();

}

--------------- ConnectionManager.java----------------------

package com.surge.pool;


/**
 * @author n
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.*;
import org.apache.commons.pool.impl.GenericObjectPool;

// Referenced classes of package com.terac.board:
//            Config

public class ConnectionManager
{

    public static ConnectionManager getInstance()
    {
        return instance;
    }

    private ConnectionManager()
    {
        try
        {
            ds = setupDataSource();
        }
        catch(Exception e) { }
    }

    public static DataSource setupDataSource()
    {
        try
        {
         String driver=Config.getInstance().getDatabaseDriver();
         System.out.println("driver is:"+driver);
            Class.forName(driver);
        }
        catch(ClassNotFoundException e) { }
        org.apache.commons.pool.ObjectPool connectionPool = new GenericObjectPool();
        org.apache.commons.dbcp.ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(Config.getInstance().getDatabaseUrl(), Config.getInstance().getDatabaseUser(), Config.getInstance().getDatabasePassword());
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
        PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
        return dataSource;
    }

    public DataSource getDataSource()
        throws SQLException
    {
        return ds;
    }

    private static final ConnectionManager instance = new ConnectionManager();
    private DataSource ds;

}

------------------db_config.xml-----------------------



   
       com.mysql.jdbc.Driver
       jdbc:mysql://localhost:3306/et?autoReconnect=ture
        et
        e.t.100
   

 

-------------------------test1.java-----------------------------

package com.surge.pool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class test1 {

 public test1() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  
  try 
  {
   conn = ConnectionManager.getInstance().getDataSource().getConnection();
   stmt = conn.createStatement();
   String sql="select count(*) as total from cs_bill";
   rs=stmt.executeQuery(sql);
   if(rs.next())
    System.out.println("total="+rs.getInt(1));
   stmt.close();
   conn.close();
  }
  catch(SQLException ex){
   System.out.println(ex);
  }
  finally
        {
            if(conn != null)
            { try
                {
                    conn.close();
                }
                catch(SQLException e) { }
            }
            if(stmt != null)
            { try
                {
                    stmt.close();
                }
                catch(SQLException e) { }
            }
            if(rs != null)
            { try
                {
                    rs.close();
                }
                catch(SQLException e) { }
            }
        }
 }

}

 

你可能感兴趣的:(java之基础部分)