Spring JDBC Complete Introduction

Spring » on Feb 9, 2012 { 40 Comments } By Sivateja

Friends, read this story carefully before you enter into spring JDBC. don’t dare to move further with out knowing this concept 🙂 ,its almost complete introduction to spring JDBC module.

Spring JDBC

Normal JDBC technology will be involved either directly or indirectly for getting a connection with database, regardless of type of framework/technology. Without using JDBC we cannot able to connect with the databases using java only.

But there are some problems if a java programmer is a directly work with JDBC

  • JDBC technology exceptions are checked, so we must use try, catch blocks in the code at various places which increases the complexity of the application. And that to this may cause to have lot of repetitive code to perform the database operations [ some thing like we may need to write loading driver, connection, creating statement lot of times ]
  • In JDBC if we open the connection with database, we only responsible to close that connection. If not we may get some connection issues
  • If you see JDBC, it will throws error codes of the database, when ever an exception is raised. In fact all java programmers may or may not know these code right ?, that to these error codes are different from one database to other database, so finally our application is gonna be database dependent

In order to overcome the above problems by using JDBC directly, Spring framework has provided one abstraction layer on top of existing JDBC technology. We used to call this layer as Spring-JDBC. In this layer spring programmers will work with this abstraction layer and that layer will internally uses JDBC technology 🙂

So spring-JDBC layer will take cares about connection management and error managements, and programmers will concentrate on their logics bla bla.

Even spring framework has provided an exception translator and it translates the checked exceptions obtained using JDBC to un-checked exceptions of spring type and finally the un-checked exceptions are thrown to java programmer, while working with spring-JDBC, the programmer no need to open and close the database connection and it will be taken care by the spring framework.

A java application can get connection with database using following 2 ways

  • By using java.sql.DriverManager [ Class ]
  • By using javax.sql.DataSource [ Interface ]

Spring framework uses DataSource interface to obtain the connection with database internally, i mean we will use any one of the following 2 implementation classes of DataSource interface.

  • Org.springframework.jdbc.datasource.DriverManagerDataSource [ class ]
  • Org.apache.commons.dbcp.BasicDataSource [ class ]

The above 2 classes are suitable when our spring application is at developing stage, in real time programmers uses connection pooling service provided by the application server, hope you know this fact 😉

Leave it, In above 2 classes DriverManagerDataSource is given by spring framework and it is equal to DriverManager class, it means spring framework internally opens a new connection and closes the connection for each operation done on the database. BasicDataSource is given the apache, and this is better than DriverManagerDataSource because BasicDataSource having inbuilt connection pooling implementation.

In spring config we need to configure the following 4 properties to obtain connection with database


<bean id=”id1” class=”org.springframework.datasource.DriverManagerDataSource”>
                           [ or ]
<bean id=”id1” class=” org.apache.commons.dbcp.BasicDataSource”>
     <property name=”driverClassName” value=”” />
     <property name=”url” value=”” />
     <property name=”username” value=”” />
     <property name=”password” value=”” />

JdbcTemplate Class In Spring-JDBC

  • JdbcTemplate class is given in org.springframework.jdbc.core.* package and this class will provides methods for executing the SQL commands on a database
  • JdbcTemplate class follows template design pattern, where a template class accepts input from the user and produces output to the user by hiding the interval details [ Confused….?? If so forget about this point 🙂 ]

JdbcTemplate class provided the following 3 type of methods to execute SQL operations on the database

  • execute()
  • update()
  • query() methods….

Note: execute and update methods are for non-select operations on the database, and query method is for select operations on the database.

JdbcTemplate class depends on DataSource object only, as it will opens database connection internally with DataSource. So we must give this DataSource object to JdbcTemplate, actually we have both setter, constructor injections in JdbcTemplate class for inserting DataSource object.

Spring config file if we insert DriverManagerDataSource object into JdbcTemplate class with constructor injection

<bean id=”id1” class=”org.springframework.datasource.DriverManagerDataSource”>

     <property name=”driverClassName” value=”” />
     <property name=”url” value=”” />
     <property name=”username” value=”” />
     <property name=”password” value=”” />


<bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">

     <constructor-arg ref="id1" />


Spring config file if we insert DriverManagerDataSource object into JdbcTemplate class with setter injection

<bean id=”id1” class=”org.springframework.datasource.DriverManagerDataSource”>

     <property name=”driverClassName” value=”” />
     <property name=”url” value=”” />
     <property name=”username” value=”” />
     <property name=”password” value=”” />


<bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">

     <property name="dataSource" ref="id1" />


Use any thing you want…..!!!
OMG seems you are little confused  or  ?  🙂  no problem just read this story and you will be able to feel confident once you go through the first Spring – JDBC program, as its practical.


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


40 Responses to “Spring JDBC Complete Introduction”
  1. Srinivasa Reddy Challa says:


    This tutorial is very nice and useful for me. Thanks for this good material provided by your website.

  2. Java4s says:

    @Srinivasa Reddy Challa

    Thank you 🙂

  3. Anitha says:

    For me IT seems that you are exactly in the same route as i prefer to learn, i mean that in the end of this chapter i was little bit confused and it says so. Even though i am reading this i get the same effect as attending a real training. Hats Off to your hard work.

  4. Java4s says:


    You welcome, thanks for your feedback 🙂

  5. Ramu says:

    Upto I didn,t see this type of good material.

  6. Deepu says:

    You r doing a excellent job please continue, post more topics on Spring.You have made topics so simple to understand.If possible please do cover EJB.

  7. Boss, Simply I became your fan after reading your tutorials..
    Awesome bro! Thanks alot for your effort.

  8. Very useful content for beginners

  9. arpitha says:

    Really very helpfulll….thanks alot

  10. suresh says:

    i am suresh. from this site i learned more knowledge. i practiced semi mail application. but i did not get any results. please verify once that application. anyway thank you

  11. raj says:

    Simple and nice tutorial.

  12. Sudhakar says:

    Really Nice Explanation. I have gone through some other sites for Spring-Jdbc, but this site is really fantastic.

  13. hari krishna says:

    Nice tutorial . Sir could you provide this tutorial with annotations also?

  14. amit says:

    I Have no words to say… its really awesome learning material… Great job!!! 🙂

  15. Lochan kumar khuntia says:

    I go through your spring jdbc blog, it simple nice. Would you kindly send me any tutorial chapter wise over spring jdbc and jdbcTemplate.

    For this kind of act i will remain ever grateful you.
    Thanking you sir.

  16. rambo says:


    thanx a lot bhai

  17. Manoj says:

    Sir, Hats of…
    Really useful notes and provided/explained in very good way…

  18. Yesha says:

    AWESOMEEEE… very easy to understand!!! keep it up! 🙂

  19. Pavan says:

    Can you please explain an example using Springs hibernate annotations.

  20. Kishor says:

    One of the best Java Tutorial Site..
    Spring Framework explanation was good.
    Easy to understand..
    Brief Explanation..and also informative..
    Keep it up Sivateja Kandula….

    Thank you so much for providing such nice explanation.


  21. sahana says:

    Seriously,this spring MVC tutorial is very easy to understand..Thanks for providing so much information in a sequential manner.

  22. Ramya says:

    Great work…..Very easy for beginners like me..Thanks a lot!!:)

  23. Trilochan Tarai says:

    Very nice explanation…definitely it will give a gud concept to freshers n experienced…really i enjoyedby going through this such nice materials…

  24. Ranjith says:

    Can you Please provide JSP as well?

  25. ramu says:

    Very Nice…Material..It helped me a lot..

  26. ramu says:

    Very Good Material…It helped Me a lot….

    Thank you

  27. shekhar says:

    please provide full programming part and explanation

  28. Dasari says:

    Very Good information. Thank you !

  29. yamini says:

    very good information thank you

  30. Anil says:

    Awesome Tutorial…Thank u!!!

  31. Karpagam says:

    This is what exactly i want.. Simple explanation with detail understanding.. Thank you so much:)

  32. Gaurav says:

    Good Explanation.

  33. prasanna kumar says:

    good explanation and good way of explaining to learners, i really thankful to your team……
    you people will get success in future and make us experts in this field

  34. Nikhitha says:

    Excellent explanation of basic concepts since basics makes the strong foundation of any concept

  35. Arun Singh says:

    Beautiful explanation. Thanks so much sir

  36. pramod sahni says:

    Very good explanation. Thank you very much.

  37. Salman Siddiqui says:

    Thanks for such an easy to understand material.

  38. Punam says:

    Very nice tutorial….thank you so much

  39. Veeru says:

    Very helpful site for beginers

  40. praveen says:

    Awesome buddy


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.