Spring JDBC configuration

This post explains the Spring JDBC configuration for accessing database in any Spring application. One or the most commonly used framework in java Spring gives support for ORM and JDBC, in this part we will look into configuration of JDBC with Spring.

To achieve this we only need one configuration file and a class containing main method. Here, configuration file is most important as we need to configure all the properties of any database like database name, database URL, username, password etc.

Step 1: Create a Java Project in eclipse, and add the required dependencies to its build path. We are using Spring 4 jars and MySQL connector jar for this example. At the end, we will end up creating project structure like-


Step 2: Jars added in the project build path are listed in below image.


Step 3: Now create a configuration file, say configuration.xml. This file contains the bean creation for MySQL database connection object.

Step 4: Now lets create a Java Class containing main function. In this class first we need to load the configuration file so that we can use the beans created in configuration file. Here we have created a DataSource object with name “dataSource” in config file and now we will use this object to get the connection to MySQL database and then get the result set. Below is the example to fetch the records from table Students and print.

Execute the program. The output on console will be like-


Hope this helps.


Nikhil Naoghare.

Leave a Reply

Your email address will not be published. Required fields are marked *

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