Connect database using Spring JdbcTemplate

This tutorial show you how to connect database using Spring JdbcTemplate. It is a important part of the Srping JDBC. It takes care of managing JDBC resources such as creating and closing of database connection. It also handles the exception and provides and returns the informative messages. Now you can build a application to connect database using Spring JdbcTemplate. It performs the operations with the database by the support of JdbcTemplate class such as insertion, updation, deletion and retrieval of the data from your database.

Here, this tutorial uses the Oracle11g database for creating the following table.

The spring application is built by three steps:

1) Create the class
This class is implemented such as below.

2) Create the class
This class has one property jdbcTemplate and three methods. The implementation of class such as below:

3) Create the spring configuration applicationContext.xml
To do with the database, you should configure spring context with three beans such as below:

Firstly, the bean DriverManagerDataSource is defined with the information about the database such as driver class name, connnection URL, username and password.

Secondly, the bean JdbcTemplate is defined to refer to the bean DriverManagerDataSource

Third, the bean FruitDao is defined to refer to the bean JdbcTemplate

You should implement the class main with name SpringJdbcTemplateSample to do the oprations using Jdbc Template. Firstly, you get the FruitDao bean from spring context then call the saveFruit() method, and you could uncomment to run two methods updateFruit() and deleteFruit() as well.

Finally, this tutorial show you the completely example how to connect database using Spring JdbcTemplate. If you have any opinion or questions about it, please leave a comment.

Happy learning!

2 thoughts on “Connect database using Spring JdbcTemplate”

  1. Thіs design is wiϲked! You definitely know how to keеp
    a reader entertained. Betաeen your wit and your videοs, I ᴡas aⅼmost moveɗ to
    start my own blog (well, almost…HaHa!) Wоndeгful jоb.
    I really loᴠed what уou had to say, and more than that,
    hoѡ you presented іt. Too cooⅼ!

Leave a Comment


Please share it if you found this useful
Hide Buttons