Home Tutorial Spring Spring3 Jdbc Spring datasource DBCP

 
 

Share on Google+Share on Google+
Spring datasource DBCP
Posted on: August 20, 2010 at 12:00 AM
Advertisement
In this section, we will discuss about Spring data source DBCP and it's implementation with example.

Spring data source DBCP

In Spring , we connect to a database via data source. When we are using jdbc layer in Spring , JNDI provide us data source or you can configure your own data source by implementing connection pool , provided by a third party. One of the most famous implementations are Apache Jakarta Commons DBCP and C3P0.The "DriverManagerDataSource" perform poorly when multiple requests for a connection are made.

Tomcat server has inbuilt DBCP support. But you must take care of path in xml file.

DBCP Configuration (xml file):

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>


EXAMPLE :

CreateTable.java
package net.roseindia;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class CreateTable {
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public void doExecute() {
    this.jdbcTemplate.execute("create table mytable(id integer, name varchar(100))");
    }
    public void doInsert() {
    this.jdbcTemplate.update("insert into mytable(id, name) values (?, ?)",1, "Ankit");
   }
}

MainCreateTable.java

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

public class MainCreateTable {

public static void main(String[] args) 
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("DataTable.xml")) ;

CreateTable myBean =(CreateTable) beanFactory.getBean("CreateTable");
myBean.doExecute();
myBean.doInsert();

}

}

DataTable.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="CreateTable" class="net.roseindia.CreateTable">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<context:property-placeholder location="jdbc.properties" />
</beans>

OUTPUT

Console Message (eclipse) :

 

Mysql:

Download Source Code

Advertisement

Related Tags for Spring datasource DBCP:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com