=>We can use DBCPConnection pooling in standalone applications or web based application to connect with database
=>Here i am using mysql db
=>First download dbcp related jars/add dependencies for maven
=>Write the code to create connection pooling
pom.xml for Maven Dependencies
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
Program:
import java.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
/**
*
* @author ramakrishna.v
*
*/
public class DBCPWithMySQLExample {
public static void main(String[] args) {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/ramakrishna");
bds.setUsername("root");
bds.setPassword("root");
try {
Connection con = bds.getConnection();
System.out.println("MaxActive:::::::::"+bds.getMaxActive());
System.out.println("MaxIdle:::::::::::"+bds.getMaxIdle());
System.out.println("Initial Size:::::::::::"+bds.getInitialSize());
System.out.println("Present no.of Active::::::::"+bds.getNumActive());
System.out.println("Present Idle:::::::::::"+bds.getNumIdle());
bds.setMaxActive(25);
bds.setMaxIdle(10);
bds.setInitialSize(5);
System.out.println("MaxActive:::::::::"+bds.getMaxActive());
System.out.println("MaxIdle:::::::::::"+bds.getMaxIdle());
System.out.println("Initial Size:::::::::::"+bds.getInitialSize());
System.out.println("Present no.of Active::::::::"+bds.getNumActive());
System.out.println("Present Idle:::::::::::"+bds.getNumIdle());
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
MaxActive:::::::::8
MaxIdle:::::::::::8
Initial Size:::::::::::0
Present no.of Active::::::::1
Present Idle:::::::::::0
MaxActive:::::::::25
MaxIdle:::::::::::10
Initial Size:::::::::::5
Present no.of Active::::::::1
Present Idle:::::::::::0
=>Here i am using mysql db
=>First download dbcp related jars/add dependencies for maven
=>Write the code to create connection pooling
pom.xml for Maven Dependencies
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
Program:
import java.sql.*;
import org.apache.commons.dbcp.BasicDataSource;
/**
*
* @author ramakrishna.v
*
*/
public class DBCPWithMySQLExample {
public static void main(String[] args) {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/ramakrishna");
bds.setUsername("root");
bds.setPassword("root");
try {
Connection con = bds.getConnection();
System.out.println("MaxActive:::::::::"+bds.getMaxActive());
System.out.println("MaxIdle:::::::::::"+bds.getMaxIdle());
System.out.println("Initial Size:::::::::::"+bds.getInitialSize());
System.out.println("Present no.of Active::::::::"+bds.getNumActive());
System.out.println("Present Idle:::::::::::"+bds.getNumIdle());
bds.setMaxActive(25);
bds.setMaxIdle(10);
bds.setInitialSize(5);
System.out.println("MaxActive:::::::::"+bds.getMaxActive());
System.out.println("MaxIdle:::::::::::"+bds.getMaxIdle());
System.out.println("Initial Size:::::::::::"+bds.getInitialSize());
System.out.println("Present no.of Active::::::::"+bds.getNumActive());
System.out.println("Present Idle:::::::::::"+bds.getNumIdle());
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
MaxActive:::::::::8
MaxIdle:::::::::::8
Initial Size:::::::::::0
Present no.of Active::::::::1
Present Idle:::::::::::0
MaxActive:::::::::25
MaxIdle:::::::::::10
Initial Size:::::::::::5
Present no.of Active::::::::1
Present Idle:::::::::::0
No comments:
Post a Comment