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