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!)
		} catch (Exception e) {
		try {

			// setup the connection pool
			BoneCPConfig config = new BoneCPConfig();
			config.setJdbcUrl("jdbc:hsqldb:mem:test"); // jdbc url specific to your database, eg jdbc:mysql://
			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.
					System.out.println(rs.getString(1)); // should print out "1"'
			connectionPool.shutdown(); // shutdown connection pool.
		} catch (SQLException e) {
		} finally {
			if (connection != null) {
				try {
				} catch (SQLException e) {
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">
	<name>Example JDBC Project</name>
	<description>Example JDBC project</description>
	<!-- This is the HSQLDB database in use by this test. Replace with MySQL, PostgreSQL etc here if you wish. -->
			<name>BoneCP Repository</name>