package com.mindprod.example;
import org.apache.tomcat.dbcp.dbcp.DriverManagerConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolingDataSource;
import org.apache.tomcat.dbcp.pool.impl.GenericObjectPool;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Demonstrate the how to connect to a MySQL Server Database with JBDC and connection pooling.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-09-17
* @since 2007-09-17
*/
public class ConnectJDBCMySQLPooling
{
/**
* which database
*/
private static final String DATABASENAME = "squirrels";
/**
* access PASSWORD
*/
private static final String PASSWORD = "sesame";
/**
* login name of user
*/
private static final String USERNAME = "charlie";
/**
* The connection. Handle to the database
*/
private static Connection conn;
/**
* connect to the database
*/
private static Connection connect() throws SQLException
{
final GenericObjectPool cp = new GenericObjectPool( null );
final DriverManagerConnectionFactory dmcf = new DriverManagerConnectionFactory( "jdbc:mysql://localhost/" +
DATABASENAME,
USERNAME,
PASSWORD
);
final PoolableConnectionFactory pcf = new PoolableConnectionFactory( dmcf, cp, null, null, false, true );
final PoolingDataSource ds = new PoolingDataSource( cp );
return ds.getConnection();
}
/**
* initialise the database
*
* @param args not used
*/
public static void main( String[] args ) throws SQLException
{
conn = connect();
conn.close();
}
}