SESSION – 5 & 6 By → Anuj Kumar Singh
What is Spring DAO (Data Access Object) DAO is used to read and write data to and from database respectively.The table below shows the responsibility of Spring and User. Note : The Spring Framework takes care of all the low-level details that can make JDBC such a tedious API to develop with
Action Spring User Define Connection Parameter Yes Open the Connection Yes Specify the SQL statement Yes Declare parameters and provide parameter values Yes Prepare and Execute the Statement Yes Set up the loop to iterate through the result (if any) Yes Do the work for each iteration Yes Process any Exception Yes Handle Transactions Yes Close the Connection,Statement and ResultSet Yes
Steps for DAO We have to follow three steps while configuring the Spring Context in XML. Step 1: Configure Data Source Driver Based Data Source Step 2: Configure JDBC Template JDBC Template Step 3: Configure custom DAO Class
Step 1: Configure Data Source The basic steps of JDBC Connection in Java, first we load the driver using Class.forName(), then getting connection using DriverManager providing the URL such as Connection con= DriverManager.getConnection(, ,) and then using Statement or PreparedStatement. In Spring we need to pass connection details (such as DriverName, URL, Username, Password) to Spring framework. The benefit of configuring data sources in this way is that they can be managed completely external to the application, leaving the application to simply ask for a data source when it’s ready to access the database.
Driver Based Data Source It is the simplest data source that can be configured in Spring. There are Two data source classes. • DriverManagerDataSource • SingleConnectionDataSource The basic difference is DriverManagerDataSource provides a new connection each time, where as SingleConnectionDataSource provides the same connection. Let’s see an example, to connect MySQL database I am using com.mysql.jdbc.Driver class. We use DriverManagerDataSource so the configuration would be:
Your IOC of spring bean for database connection looks like
How it is if we take the connection details (Driver class name, url, username, password etc. ) from a property file rather than defining in Spring Context XML file itself? Yes, it is a good idea to take the database connection details from a property file. So let’s create a property file. We will name the file as: jdbc.properties
You need this extra entry to include the properties from jdbc.properties file. Properties define in jdbc.properties file
Step 2: Configure JDBC Template After Driver Based Data Source the next step is JdbcTemplate. It is the central class in the JDBC core package. It simplifies the use of JDBC since it handles the creation and release of resources. This helps to avoid common errors such as forgetting to always close the connection. This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.
In Spring IOC container and given JdbcTemplate to DAOs as a bean reference.
Step 3: Configure custom DAO Class We already discussed that the JdbcTemplate can be used within a DAO implementation via direct instantiation with a DataSource reference OR be configured in a Spring IOC container and given to DAOs as a bean reference. Now we will see how they can be implemented in the DAO class. Configured in Spring IOC container and given JdbcTemplate to DAOs as a bean reference. In all our examples, we adopted this option only…
In DAO Class Bean id must be JDBCTemplate JDBCTemplate not DataSource
Spring DAO  Example 1 : Inserting data in database by Test class a. POJO class
b. DAO class
c. Test class
d. Xml File
Example 2  Inserting data in database with the Xml File a. POJO class
b. DAO class
c. Test class
d. Xml File
Example 3Data Insertion and retrieval from Database
We can apply restriction also on the data access based on any particular email. String email=“anujkumarsingh@gmail.com”; Then in query pass it like “Select * from emps where email= ‘ ”+email+” ’ ”;
Spring framework DAO
Spring framework DAO

Spring framework DAO

  • 1.
    SESSION – 5& 6 By → Anuj Kumar Singh
  • 2.
    What is SpringDAO (Data Access Object) DAO is used to read and write data to and from database respectively.The table below shows the responsibility of Spring and User. Note : The Spring Framework takes care of all the low-level details that can make JDBC such a tedious API to develop with
  • 3.
    Action Spring User DefineConnection Parameter Yes Open the Connection Yes Specify the SQL statement Yes Declare parameters and provide parameter values Yes Prepare and Execute the Statement Yes Set up the loop to iterate through the result (if any) Yes Do the work for each iteration Yes Process any Exception Yes Handle Transactions Yes Close the Connection,Statement and ResultSet Yes
  • 4.
    Steps for DAO Wehave to follow three steps while configuring the Spring Context in XML. Step 1: Configure Data Source Driver Based Data Source Step 2: Configure JDBC Template JDBC Template Step 3: Configure custom DAO Class
  • 5.
    Step 1: ConfigureData Source The basic steps of JDBC Connection in Java, first we load the driver using Class.forName(), then getting connection using DriverManager providing the URL such as Connection con= DriverManager.getConnection(, ,) and then using Statement or PreparedStatement. In Spring we need to pass connection details (such as DriverName, URL, Username, Password) to Spring framework. The benefit of configuring data sources in this way is that they can be managed completely external to the application, leaving the application to simply ask for a data source when it’s ready to access the database.
  • 6.
    Driver Based DataSource It is the simplest data source that can be configured in Spring. There are Two data source classes. • DriverManagerDataSource • SingleConnectionDataSource The basic difference is DriverManagerDataSource provides a new connection each time, where as SingleConnectionDataSource provides the same connection. Let’s see an example, to connect MySQL database I am using com.mysql.jdbc.Driver class. We use DriverManagerDataSource so the configuration would be:
  • 7.
    Your IOC ofspring bean for database connection looks like
  • 8.
    How it isif we take the connection details (Driver class name, url, username, password etc. ) from a property file rather than defining in Spring Context XML file itself? Yes, it is a good idea to take the database connection details from a property file. So let’s create a property file. We will name the file as: jdbc.properties
  • 9.
    You need thisextra entry to include the properties from jdbc.properties file. Properties define in jdbc.properties file
  • 10.
    Step 2: ConfigureJDBC Template After Driver Based Data Source the next step is JdbcTemplate. It is the central class in the JDBC core package. It simplifies the use of JDBC since it handles the creation and release of resources. This helps to avoid common errors such as forgetting to always close the connection. This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.
  • 11.
    In Spring IOCcontainer and given JdbcTemplate to DAOs as a bean reference.
  • 12.
    Step 3: Configurecustom DAO Class We already discussed that the JdbcTemplate can be used within a DAO implementation via direct instantiation with a DataSource reference OR be configured in a Spring IOC container and given to DAOs as a bean reference. Now we will see how they can be implemented in the DAO class. Configured in Spring IOC container and given JdbcTemplate to DAOs as a bean reference. In all our examples, we adopted this option only…
  • 13.
    In DAO Class Beanid must be JDBCTemplate JDBCTemplate not DataSource
  • 14.
    Spring DAO Example 1 : Inserting data in database by Test class a. POJO class
  • 15.
  • 16.
  • 17.
  • 18.
    Example 2 Inserting data in database with the Xml File a. POJO class
  • 19.
  • 20.
  • 21.
  • 22.
    Example 3Data Insertionand retrieval from Database
  • 24.
    We can applyrestriction also on the data access based on any particular email. String email=“anujkumarsingh@gmail.com”; Then in query pass it like “Select * from emps where email= ‘ ”+email+” ’ ”;