Jun 18, 2012

Spring DAO


Objective:
Lets create an example for Spring DAO using jnidiTemplate.

1) Start the Hypersonic SQL DB & Create the "contacts" table in Hypersonic SQL
Refer to the following for creating the table in Hypersonic SQL -
                 a) For configuring & starting the HSQL Server
                 b) For creating the "contacts" table in HSQL Server


3) Create the Java Components:
                a)Create the Model: Contact.java

package com.dct.phonebook.model;
public class Contact {
      private String name;
      private int phone;
     
      public Contact(String name, int phone){
            this.name = name;
            this.phone = phone;
      }    
      public String getName() {
            return name;
      }    
      public void setName(String name) {
            this.name = name;
      }    
      public int getPhone() {
            return phone;
      }    
      public void setPhone(int phone) {
            this.phone = phone;
      }
}

              b)Create the DAO contract(interface): ContactsDAO.java


package com.dct.phonebook.dao;
import com.dct.phonebook.model.Contact;

public interface ContactsDAO {
      public void addContact(final Contact contact) throws Exception;  
}

            c)Create the implementation for the contract: ContactsDAOImpl.java

package com.dct.phonebook.dao.impl;
import java.sql.Connection;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import com.dct.phonebook.dao.ContactsDAO;
import com.dct.phonebook.model.Contact;

public class ContactsDAOImpl implements ContactsDAO {
     
      private DataSource dataSource;
      private JdbcTemplate jdbcTemplate;
     
      public DataSource getDataSource() {
            return dataSource;
      }

      public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
      }

      public void addContact(final Contact contact) throws Exception {       
            Connection conn = null;      
            try{
                
                String sql = "insert into contacts(name,phone) values(?,?)";
                jdbcTemplate = new JdbcTemplate(dataSource);         

                jdbcTemplate.update(sql, new Object[]{contact.getName(),     contact.getPhone()});          
           
            } catch(Exception e){              
                  throw e;
            } finally {
                  if(conn!=null){
                        try{
                              conn.close();
                        } catch(Exception e){
                              throw e;
                        }
                  }
            }
      }    
}
           d)Create the Spring configuration files:
                   - Configure the dataSource : spring-datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.hsqldb.jdbcDriver" />      
            <property name="url" value="jdbc:hsqldb:http://localhost/phonebook"/>
            <property name="username" value="SA" />
            <property name="password" value="" />
      </bean>

</beans>
 
                - Configure the Model: spring-model.xml
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <bean id="contactsDAO" class="com.dct.phonebook.dao.impl.ContactsDAOImpl">
            <property name="dataSource" ref="dataSource" />
      </bean>

</beans>

               - Create the Application Context file: spring-phonebook.xml
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

      <import resource="datasource/spring-datasource.xml" />
      <import resource="model/spring-model.xml" />

</beans>

                e) Create the test class for testing this application: Phonebook.java

package com.dct.phonebook;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dct.phonebook.dao.ContactsDAO;
import com.dct.phonebook.model.Contact;


public class Phonebook {
     
    public static void main(String[] args) throws Exception {
      ApplicationContext context =
            new ClassPathXmlApplicationContext("resources/spring-Phonebook.xml");

        ContactsDAO contactsDAO = (ContactsDAO) context.getBean("contactsDAO");
        Contact contact = new Contact("renganath", 97890);
        contactsDAO.addContact(contact);                                                                                                                                                                                                                                             
   }   
}

The folder structure of the final application is:


No comments:

Post a Comment