How do I create a Data Source object for JdbcTemplate?

In this example you will learn how to create and configure a DriverManagerDataSource object that will be used by the JdbcTemplate object. There are some information required when creating a DataSource including the JDBC driver class, the JDBC Url of the target database, the username and password for connecting to database server.

package org.kodejava.spring.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

public class DataSourceDemo {
    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String JDBC_URL = "jdbc:mysql://localhost/musicdb";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "";

    public static void main(String[] args) {
        // Creates an instance of DriverManagerDataSource and pass
        // it to the JdbcTemplate.
        DataSource source = getDataSource();
        JdbcTemplate template = new JdbcTemplate(source);

        // After creating a template with a data source inject we
        // can do a database manipulation such as the CRUD operation.
        System.out.println("DataSource = " + template.getDataSource());
        List<Map<String, Object>> records = template.queryForList("SELECT * FROM record");
        for (Map<String, Object> record : records) {
            System.out.println("Records = " + record);
        }
    }

    /**
     * Returns a DataSource object for connection to the database.
     *
     * @return a DataSource.
     */
    private static DataSource getDataSource() {
        // Creates a new instance of DriverManagerDataSource and sets
        // the required parameters such as the Jdbc Driver class,
        // Jdbc URL, database username and password.
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(DataSourceDemo.DRIVER);
        dataSource.setUrl(DataSourceDemo.JDBC_URL);
        dataSource.setUsername(DataSourceDemo.USERNAME);
        dataSource.setPassword(DataSourceDemo.PASSWORD);
        return dataSource;
    }
}

Maven Dependencies

<dependencies>
    <!-- https://search.maven.org/remotecontent?filepath=org/springframework/spring-jdbc/5.3.21/spring-jdbc-5.3.21.jar -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.21</version>
    </dependency>
    <!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.29/mysql-connector-java-8.0.29.jar -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.29</version>
    </dependency>
</dependencies>

Maven Central Maven Central

2 Comments

  1. Hi Raman,

    One solution is to encrypt the database access details (url, username, password). Extends the DriverManagerDataSource class to create a version that decrypt all the encrypted database access details.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.