JDBC Example

The following Java example demonstrates the use and configuration of BoneCP in a simple JDBC environment. This example simply fetches a JDBC connection from the connection pool and runs a test statement.

 

The full source code of this example project can be found here: You will need Apache Maven to automatically include the required jar files into your classpath. Alternatively you may add the required JAR files manually.
 
 
package com.jolbox;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
 
/** A test project demonstrating the use of BoneCP in a JDBC environment.
 * @author wwadge
 *
 */
public class ExampleJDBC {
 
	/** Start test
	 * @param args none expected.
	 */
	public static void main(String[] args) {
		BoneCP connectionPool = null;
		Connection connection = null;
 
		try {
			// load the database driver (make sure this is in your classpath!)
			Class.forName("org.hsqldb.jdbcDriver");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
		try {

			// setup the connection pool
			BoneCPConfig config = new BoneCPConfig();
			config.setJdbcUrl("jdbc:hsqldb:mem:test"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
			config.setUsername("sa"); 
			config.setPassword("");
			config.setMinConnectionsPerPartition(5);
			config.setMaxConnectionsPerPartition(10);
			config.setPartitionCount(1);
			connectionPool = new BoneCP(config); // setup the connection pool
			
			connection = connectionPool.getConnection(); // fetch a connection
			
			if (connection != null){
				System.out.println("Connection successful!");
				Statement stmt = connection.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS"); // do something with the connection.
				while(rs.next()){
					System.out.println(rs.getString(1)); // should print out "1"'
				}
			}
			connectionPool.shutdown(); // shutdown connection pool.
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
	
 
If you use Maven, the following should be sufficient to pull in the JAR files required into your classpath:
 
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jolbox</groupId>
	<artifactId>examplejdbc</artifactId>
	<name>Example JDBC Project</name>
	<version>1.0.0</version>
	<description>Example JDBC project</description>
	<dependencies>
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp</artifactId>
			<version>0.6.5</version>
			<scope>compile</scope>
		</dependency>
	<!-- This is the HSQLDB database in use by this test. Replace with MySQL, PostgreSQL etc here if you wish. -->
		<dependency>
			<groupId>hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>1.8.0.10</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>
	
	<repositories>
		<repository>
		  <releases>
                <enabled>true</enabled>
            </releases>
			<id>bonecp-repo</id>
			<name>BoneCP Repository</name>
			<url>http://jolbox.com/bonecp/downloads/maven</url>
		</repository>
	</repositories>
</project>