Newsletter |
Spring JDBC Complete Introduction
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=”” /> </bean>
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> <bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="id1" /> <bean>
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> <bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="id1" /> <bean>
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 .:: | ||
Hi,
This tutorial is very nice and useful for me. Thanks for this good material provided by your website.
@Srinivasa Reddy Challa
Thank you 🙂
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
@Anitha
You welcome, thanks for your feedback 🙂
Upto I didn,t see this type of good material.
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.
Boss, Simply I became your fan after reading your tutorials..
Awesome bro! Thanks alot for your effort.
Very useful content for beginners
thnkuuu
Really very helpfulll….thanks alot
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
Simple and nice tutorial.
Really Nice Explanation. I have gone through some other sites for Spring-Jdbc, but this site is really fantastic.
Nice tutorial . Sir could you provide this tutorial with annotations also?
I Have no words to say… its really awesome learning material… Great job!!! 🙂
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.
polichu
thanx a lot bhai
Sir, Hats of…
Really useful notes and provided/explained in very good way…
AWESOMEEEE… very easy to understand!!! keep it up! 🙂
Can you please explain an example using Springs hibernate annotations.
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.
Regards,
Kishor
Seriously,this spring MVC tutorial is very easy to understand..Thanks for providing so much information in a sequential manner.
Great work…..Very easy for beginners like me..Thanks a lot!!:)
Very nice explanation…definitely it will give a gud concept to freshers n experienced…really i enjoyedby going through this such nice materials…
Can you Please provide JSP as well?
Very Nice…Material..It helped me a lot..
Very Good Material…It helped Me a lot….
Thank you
please provide full programming part and explanation
Very Good information. Thank you !
very good information thank you
Awesome Tutorial…Thank u!!!
This is what exactly i want.. Simple explanation with detail understanding.. Thank you so much:)
Good Explanation.
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
Excellent explanation of basic concepts since basics makes the strong foundation of any concept
Beautiful explanation. Thanks so much sir
Very good explanation. Thank you very much.
Thanks for such an easy to understand material.
Very nice tutorial….thank you so much
Very helpful site for beginers
Awesome buddy