Srikanth Technologies

Songs Project using Web, DAO and Spring + JDBC

This is another application to store details of songs using a Web application that uses JDBC through Spring Core. This application is same as the previous one, Songs With Hibernate, in terms of Database and View, but it uses JDBC through Spring. Here are the steps to create this application.

Create Web Application

Use NetBeans (or any other IDE of your choice) to create a new web application. It uses Tomcat. Again you can choose whatever server you like.

I am naming the project as SongsSpringJdbc as we us Spring to access database through JDBC.

Add Spring Framework 3.0 library and JSTL library to project. Also include OJDBC14.jar to libraries as it contains Jdbc driver for Oracle.

In order to use AOP with Spring (we use AOP for transaction management), you need to add the following .jar files:

I have made all these files available as a single download at http://www.megafileupload.com/en/file/311042/springdeplib-rar.html. So download and add these .jar files to your project libraries so that they are placed in the classpath.

Add the following files from previous project, Songs With Hibernate, as they remain the same for this project also.

spring/songs.xml

This is configuration file for Spring beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
           
       <bean id="oracledatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
           <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
           <property name="username" value="music" />
           <property name="password" value="music" />
       </bean>
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
           <constructor-arg ref="oracledatasource"/>
       </bean>            
       <bean id="songsbean" class="spring.SongsManagement">
           <property name="jdbcTemplate" ref="jdbcTemplate"/>
       </bean>            
       <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="oracledatasource"/>
       </bean>
       <tx:annotation-driven transaction-manager="txManager" />
</beans>

spring/SongMapper.java

This is used to map a row in Songs table to Song object.
package spring;

import entities.Song;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;


public class SongMapper implements RowMapper
{
                     public Song mapRow(ResultSet rs,int rowNum)  throws SQLException
                     {
                         Song s = new Song();
                         s.setSongid( rs.getInt("songid"));
                         s.setTitle( rs.getString("title"));
                         s.setSinger( rs.getString("singer"));
                         return s;
                     }
}

spring/SongsManagement.java

This is a bean in Spring that manages songs. It uses JdbcTemplate to access database through JDBC.

package spring;

import entities.Song;
import java.util.List;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class SongsManagement {
    private SimpleJdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(SimpleJdbcTemplate template) {
        this.jdbcTemplate = template;
    }
    @Transactional(propagation=Propagation.REQUIRED)
    public void addSong(String title, String singer ){
        jdbcTemplate.update("insert into songs values(songid.nextval,?,?)", title,singer);
    }
    public List getSongs() {
        return  jdbcTemplate.query("select * from songs order by title", new SongMapper());
    }
}

dao/SongsDAO.java

This class accesses Spring bean and calls method to add and list songs.
package dao;

import entities.Song;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.SongsManagement;

public class SongsDAO {
    public static boolean addSong(String title, String singer) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring/songs.xml");
        SongsManagement sm= (SongsManagement) ctx.getBean("songsbean");
        try {
             sm.addSong(title, singer);
             return true;
        }
        catch(Exception ex) {
            System.out.println("Error In AddSong() -->" +  ex.getMessage());
            return false;
        }
    }
    public static List getSongs() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring/songs.xml");
        SongsManagement sm= (SongsManagement) ctx.getBean("songsbean");
        return  sm.getSongs();
    }
}

That's All

As all other files remain the same, just deploy the project and run index.jsp.