Using NamedParameterJdbctemplate

We will use Ch03_JdbcTemplates to add a new class for this demonstration, with the help of the following steps:

  1. Add a BookDAO_NamedParameter class in the com.packt.ch03.dao package, which implements BookDAO, and annotate it with @Repository as we did earlier.
  2. Add NamedParameterJdbcTemplate to it as a data member, and annotate it with @Autowired.
  3. Implement the overridden methods to perform JDBC operations with the help of update().NamedParameterJdbcTemplate supports giving names to the parameters in the SQL query. Find the following query to add Book:
        String INSERT_BOOK = "insert into book values
(:bookName,:ISBN,:publication,:price,:description, :author)";
Each parameter has to be prefixed with the colon as:name_of_parameter.

If these are the names of the parameters, then these have to be registered so that the framework places them in the query. To do this, we will have to create a Map where these parameter names act as keys, whose values will be specified by the developer. The following code will give you a clear idea:

@Repository(value="BookDAO_named") 
public class BookDAO_NamedParameter implements BookDAO { 
 
  @Autowired  
  private NamedParameterJdbcTemplate namedTemplate; 
 
  @Override 
  public int addBook(Book book) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    String INSERT_BOOK = "insert into book  
      values(:bookName,:ISBN,:publication,:price, 
      :description,:author)"; 
    Map<String,Object>params=new HashMap<String,Object>(); 
    params.put("bookName", book.getBookName()); 
    params.put("ISBN", book.getISBN()); 
    params.put("publication", book.getPublication()); 
    params.put("price",book.getPrice()); 
    params.put("description",book.getDescription()); 
    params.put("author", book.getAuthor()); 
    rows=namedTemplate.update(INSERT_BOOK,params);  
    return rows; 
  } 
 
  @Override 
  public int updateBook(longISBN, intprice) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    String UPDATE_BOOK =  
      "update book set price=:price where ISBN=:ISBN"; 
 
    Map<String,Object>params=new HashMap<String,Object>(); 
    params.put("ISBN", ISBN); 
    params.put("price",price); 
    rows=namedTemplate.update(UPDATE_BOOK,params); 
   return rows; 
  } 
 
  @Override 
  public boolean deleteBook(longISBN) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    boolean flag=false; 
    String DELETE_BOOK = "delete from book where ISBN=:ISBN"; 
 
    Map<String,Object>params=new HashMap<String,Object>(); 
    params.put("ISBN", ISBN); 
    rows=namedTemplate.update(DELETE_BOOK, params); 
    if(rows>0) 
    flag=true; 
    return flag; 
  } 
} 
  1. Add a bean in connection_new.xml for NamedParameterJdbcTemplate, as follows:
<bean id="namedTemplate" 
  class="org.springframework.jdbc.core.namedparam. 
    NamedParameterJdbcTemplate"> 
  <constructor-arg ref="dataSource"/> 
</bean> 

In all the other demos, we have used the setter injection, but here, we cannot use setter injection, as the class doesn't have a default constructor. So, use the constructor dependency injection only.

  1. Use the developed MainBookDAO_operations to test how the JdbcTemplate works. You just have to update the statement, which will get the BookDAO_named bean to execute operations. The changed code will be as follows:
        BookDAO bookDAO=(BookDAO) context.getBean("BookDAO_named"); 

You can find the complete code in MainBookDAO_NamedTemplate.java.

  1. Execute the code to get the success message.

In a small Java application, the code will have a small number of DAO classes. So, handling JDBC will not be complex to the developers handling DAOs with template classes in each of them. This also leads to duplication of code. However, the complexity becomes difficult to handle when we deal with enterprise applications with more number of classes. The alternative is that instead of injecting the template class in each of the DAO, choose a parent who has the same ability as that of Template classes. Spring has JdbcDaoSupport and NamedParameterJdbcSupport as such supportive DAOs. These abstract support classes provide a common base, leaving out repetition of the code and the wiring of properties in each DAO.

Let's take the same project ahead to use supportive DAOs. We will use the JdbcDaoSupport class to understand the practical aspects. Let's follow the given steps:

  1. Add BookDAO_JdbcTemplateSupport in com.packt.ch03.dao, which extends JdbcDaoSupport and implements BookDAO.
  2. Override the methods from interface, which will deal with the database. The BookDAO_JdbcTemplateSupport class inherits JdbcTemplate from JdbcDaoSupport. So, the code remains the same as while using JdbcTemplate with a little change. The JdbcTemplate has to be accessed through the getter method, and we do not add a data member of type JdbcTemplate as we did earlier. The complete code to use JdbcDaoSupport is as follows:
@Repository(value="daoSupport") 
public class BookDAO_JdbcTemplateSupport extends JdbcDaoSupport  
implements BookDAO 
{ 
  @Autowired 
  public BookDAO_JdbcTemplateSupport(JdbcTemplate  
    jdbcTemplate) 
  { 
    setJdbcTemplate(jdbcTemplate); 
  } 
 
  @Override 
  public int addBook(Book book) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    String INSERT_BOOK = "insert into book values(?,?,?,?,?,?)"; 
 
    rows=getJdbcTemplate().update(INSERT_BOOK,  
      book.getBookName(), book.getISBN(),  
      book.getPublication(), book.getPrice(), 
      book.getDescription(), book.getAuthor()); 
  
    return rows; 
  } 
 
  @Override 
  public int updateBook(longISBN, intprice) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    String UPDATE_BOOK = "update book set price=? where  
    ISBN=?"; 
 
    rows=getJdbcTemplate().update(UPDATE_BOOK, price,ISBN); 
    return rows; 
  } 
 
  @Override 
  public boolean deleteBook(longISBN) { 
    // TODO Auto-generated method stub 
    int rows = 0; 
    boolean flag=false; 
    String DELETE_BOOK = "delete from book where ISBN=?"; 
 
    rows=getJdbcTemplate().update(DELETE_BOOK, ISBN); 
    if(rows>0) 
    flag=true; 
 
    return flag; 
  } 
} 
  1. Write the main code as we did in earlier applications. You can find the code in MainBookDAO_DAOSupport.java.
To use DAO classes, the dependencies will be injected through the constructor.

Spring facilitates the use of templates as one of the approaches to simplify the work of development. Along with this, two more approaches are provided by Spring to handle a database. Before discussing how to handle exceptions, let's first discuss the remaining approaches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.24