DriverManagerDataSouce in Spring
Spring provides many ways to establish connection to a database and perform operations such as retrieval of records, insertion of new records and updating / deletion of existing records.
The most basic of them is using DriverManagerDataSource.
This post will detail it out in depth.
For any class to be discovered by Spring, it needs to be declared in its configuration file as a bean.
Hence, in order to create a bean for DriverManagerDataSource, it is declared in Spring configuration file as :

<bean id=“dataSource”

In order to make connection to a database, some parameters such as database url, name, user name and password are required.
These parameters are supplied as properties to the above bean declaration. Hence the complete bean declaration now becomes :

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="username" value="root" />
  <property name="password" value="root" />
  <property name="url" value="jdbc:mysql://localhost:3306/codippa" /> 

DriverManagerDataSource has a getConnection() method which returns a connection of type java.sql.Connection.
This connection can then be used to create a java.sql.Statement or java.sql.PreparedStatement which then executes an SQL query in the same way as in JDBC (Java DataBase Connectivity).
Complete code follows :

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class DriverManagerDataSourceExample {

   public void fetchDataUsingDriverManagerDS() throws SQLException {
     // initialize Spring's Application context
     ApplicationContext context = 
     new ClassPathXmlApplicationContext("spring-core-config.xml");
     // get bean declared with name "dataSource" in the configuration file
     DriverManagerDataSource dataSource = (DriverManagerDataSource)
     // get connection
     Connection connection = dataSource.getConnection();
     // let's assume table name is student
     PreparedStatement preparedStatement = 
          connection.prepareStatement("Select * from student");
     ResultSet resultSet = preparedStatement.executeQuery();
     while ( {
       // student table has a column name
       String name = resultSet.getString("name");
       // student table also has a column rollno
       int rollNumber = resultSet.getInt("rollno");
       System.out.println("Name of student :: " + name);
       System.out.println("Roll Number of student ::" + rollNumber);

In the above code spring-core-config.xml is the spring configuration file which lies on the classpath.

Below is the screenshot of the file:

<beans xmlns=""

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="username" value="root" />
    <property name="url" value="jdbc:mysql://localhost:3306/codippa" />

The properties driverClassName, username, password defined in the bean declaration are the field names of the DriverManagerDataSource.

The above example would need dependencies of Spring framework and the database used.
This example has been tested on Spring framework v5.2.5 RELEASE and MySql database, so following would be the dependencies :

If you are using Maven as the build tool then the dependencies defined in the pom.xml are :

<project xmlns=""


  <!-- Spring framework -->

  <!-- MySQL database driver -->


If gradle is the build tool of your choice then below is the build.gradle file to be used :

apply plugin:'java'
apply plugin:'eclipse'

repositories {
sourceCompatibility = 1.8
targetCompatibility = 1.8 

dependencies {
 compile group:'org.springframework', name:'spring-jdbc', version:'5.2.5.RELEASE'
 compile group:'org.springframework', name:'spring-beans', version:'5.2.5.RELEASE'
 compile 'mysql:mysql-connector-java:5.1.10'

Let’s tweak in

  1. This method just changes the mechanism of retrieving connection as done in traditional jdbc way.
    You are no longer required to explicitly load the class using Class.forName()method, neither you are required to call DriverManager.getConnection()method.
    Spring creates a connection via its DriverManagerDataSource class.
  2. When using annotations, you can directly use @Autowired annotation over the dataSourcefield and retrieving it using ApplicationContext is not required, which further cuts down a couple of lines of code.
  3. DriverManagerDataSource is an implementation of javax.sql.DataSource and hence is a part of JDBC specification.
  4. For configuring DriverManagerDataSourcebean using java.util.Properties object use the following declaration:
    <bean id="dataSource"
      <property name="url" value="jdbc:mysql://localhost:3306/codippa" />
      <property name="connectionProperties">
        <prop key="user">root</prop>
        <prop key="password">root</prop>
        <prop key="driverClassName">com.mysql.jdbc.Driver</prop>

    Here connectionProperties is a field defined in class AbstractDriverBasedDataSource.
    This is the super class of DriverManagerDataSource.
    The key names (user, password and driverClassName) of this properties object will be the field names of Connection implementation class in the appropriate database driver.
    In this case it would be MySql driver.

  5. This is a very basic method of connecting to a database using Spring as it does not provide connection pooling and as such can not handle multiple simultaneous connections.
    Hence it is not recommended to be used in production environment and should be used just for testing purposes.
    For more advanced purposes, use JdbcTemplate.


Liked the article ? Spread the word...

This Post Has One Comment

  1. Aleisha Mahomet

    Здравствуйте ув. Boss!
    Предложу поднятие на 10-20 пунктов позиций в поисковиках.
    Цена всего 400 ру. Качественный ИкС. Отчётность.
    Прогоны сайтов от Профессионалов!
    Хваленые БаЗы.
    WhatsApp: +7(977)536-08-36

Leave a Reply