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 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.


This is configuration file for Spring beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       <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 id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
           <constructor-arg ref="oracledatasource"/>
       <bean id="songsbean" class="spring.SongsManagement">
           <property name="jdbcTemplate" ref="jdbcTemplate"/>
       <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="oracledatasource"/>
       <tx:annotation-driven transaction-manager="txManager" />


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;


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;
    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());


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 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.