In previous couple of posts MysqlDataSource example and DBCP Connection Pooling Example we learnt about how to take JDBC connection using MysqlDataSource and Apache DBCP connection pools
In this post we will learn how to take JDBC connection from C3P0 Connection pool.
Note:You have to add few dependencies binaries in your project class path those dependencies you can see in the blow project lib folder
Create database.properties file in src folder as shown in the above project structure and make entry of below properties.
1 2 3 4 |
driver.class.name=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/jdbcpooldb db.username = root db.password=root |
SQL Query to create employee_table in MySQL (jdbcpooldb schema)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CREATE DATABASE IF NOT EXISTS jdbcpooldb; CREATE TABLE IF NOT EXISTS `employee_table` ( `employee_id` int(11) NOT NULL auto_increment, `employee_name` varchar(60) NOT NULL, `email` varchar(45) NOT NULL, `salary` double default NULL, `date_of_joining` datetime default NULL, `bonus` decimal(10,0) default NULL, PRIMARY KEY (`employee_id`) ) ; INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) |
Once you execute above db script your database schema(jdbcpooldb) will be created and employee_table will be created with three rows as below.
Utility class which is responsible to get JDBC connection object using C3P0 DataSource connection pool With MYSQL Database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.infotech.util; import java.beans.PropertyVetoException; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; /* * @author KK JavaTutorials * Utility class which is responsible to get JDBC connection object using * C3P0 DataSource connection pool With MYSQL Database. */ public class DBUtil { private static final String DB_USERNAME="db.username"; private static final String DB_PASSWORD="db.password"; private static final String DB_URL ="db.url"; private static final String DB_DRIVER_CLASS="driver.class.name"; private static Properties properties = null; private static ComboPooledDataSource dataSource; static{ try { properties = new Properties(); properties.load(new FileInputStream("src/database.properties")); dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(properties.getProperty(DB_DRIVER_CLASS)); dataSource.setJdbcUrl(properties.getProperty(DB_URL)); dataSource.setUser(properties.getProperty(DB_USERNAME)); dataSource.setPassword(properties.getProperty(DB_PASSWORD)); dataSource.setMinPoolSize(100); dataSource.setMaxPoolSize(1000); dataSource.setAcquireIncrement(5); } catch (IOException | PropertyVetoException e) { e.printStackTrace(); } } public static DataSource getDataSource(){ return dataSource; } } |
This Client class makes use of DBUtil.getDataSource() method to take connection from C3P0 connection pool With MYSQL Database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.infotech.client; import java.math.BigDecimal; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.infotech.util.DBUtil; /* * @author KK JavaTutorials * This class makes use of DBUtil.getDataSource() method to take connection from * C3P0 connection pool With MYSQL Database. */ public class ClientTest { public static void main(String[] args) throws SQLException { try (Connection connection = DBUtil.getDataSource().getConnection(); Statement st = connection.createStatement();) { String SQL = "SELECT *FROM employee_table"; ResultSet rs = st.executeQuery(SQL); while (rs.next()) { int empId = rs.getInt("employee_id"); String eName = rs.getString("employee_name"); String email = rs.getString("email"); Double salary = rs.getDouble("salary"); BigDecimal bonus = rs.getBigDecimal("bonus"); System.out.println(empId + "\t" + eName + "\t" + salary + "\t" + email + "\t" + bonus); } } catch (Exception e) { e.printStackTrace(); } } } |
After running ClientTest.java program you will look below output on eclipse console:
Above eclipse console output shows we are able to read rows from MySQL database that means we are successfully taken database connection using C3P0 connection pool
That’s all about this topic C3P0 JDBC connection pool Example.
You May Also Like:
Why we need a database connection pooling
MysqlDataSource example
DBCP Connection Pooling Example
BoneCP connection pool example
Hikari Connection Pooling Example
If you have any feedback or suggestion please feel free to drop in blow comment box.