Thursday, June 28, 2018

Spring DAO




If already JDBC is there why we  should use  Spring DAO ?

JDBC Steps :
------------------

1.  Load the Driver class.
2.  Get the Connection Object
3.  Create the statement/prepared statement/callable statement.
4.  Prepare the SQL Query
5.  Handle exceptions.
6.  Handling Resultset
7.  Close the connection


Note :  For every Java application step 1, 2, 3, 5, 7  are common,  boiler plate code will be taking care by frame work, only has to take care of SQL Query and Handling result set.


JDBC Template  :

1.  DriverClass, Uname, pwd

Instead Developer will write  boiler plate code and closeconnection manually approach then Spring provides module DAO.
In DAO for jdbc there is a template is "JDBCTemplate".  It will take care steps 1-3 and it will also handle resources like manage the connection objects.

Also  JDBCTemplate configurations can be   take care of  in two ways:

a)  XML Approach



<beans>
    <bean id = "org.spf.dao.JDBTemplate">
                ------
                -----

      </bean>
</beans>

     
b)  Java Configuration                                                

@configuration

public class javaconfig{
@Bean
public JDBCTemplate getTemplate(){

JDBCTemplate  template = new JDBCTemplate();

return jdbcTemplate:
}



Also using JDBCTemplate  -  we can  use  SQL and PLSQL to pass SQL queries.

a)  SQL  -  Using this approach   we ca call any one of the method and pass the sql query and get he response.

queryForObject()
queryForString()
queryForInt()
queryForxxx()
....


b) PLSQL  -

a)    IN and OUT  Params  then our DAO class should extends StoredProcedure  using -   execute()
b)   IN and OUT params + resultset hen our DAO class should extends StoredProcedure and implements RowMapper

         --execute()   -  execute  the SP and provides OUT param response
         --mapRow()  -  Handling Resultset.
       

Spring DAO steps to invoke the SP :



No comments:

Post a Comment

Hyderabad Trip - Best Places to visit

 Best Places to Visit  in Hyderabad 1.        1. Golconda Fort Maps Link :   https://www.google.com/maps/dir/Aparna+Serene+Park,+Masj...