You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Spring JDBC Complete Introduction

Spring » On Feb 9, 2012 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

 

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

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

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.






By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

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

    Hi,

    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:

    HI,
    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.
    Anitha

  4. Java4s says:

    @Anitha

    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. somashekara says:

    Very useful content for beginners
    thnkuuu

  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:

    sir,
    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.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.