Spring Framework

Spring Projects

Spring Project 1

Example of Insert,update,delete and select data using Spring Template
Previous Home Next

Introduction: This example provide the facility to insert, update, delete and fetch data using spring template design pattern then provide connect to JDBC.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server

Source Code:

ConnectionProvider.java

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="ds" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/tableone"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="temp" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>
<bean id="dao" class="org.r4r.EmpTemplateDao">
<property name="template" ref="temp"/>
</bean>
</beans>

Emp.java

package org.r4r;
public class Emp {
int id;
String name,job;
int salary;
public Emp() {
super();
}
public Emp(int id, String name, String job, int salary) {
super();
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

 

EmpTemplateDao.java

package org.r4r;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
public class EmpTemplateDao {
JdbcTemplate template;
public JdbcTemplate getTemplate() {
return template;
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@SuppressWarnings("unchecked")
public List getAllEmp(){
RowMapper mapper=new RowMapper() {

@Override
public Object mapRow(ResultSet rset, int index) 
throws SQLException {
Emp e=new Emp();
e.setId(rset.getInt(1));
e.setName(rset.getString(2));
e.setJob(rset.getString(3));
e.setSalary(rset.getInt(4));
return e;
}
};
return template.query("select * from emp", mapper);
}
public void save(final Emp e){
PreparedStatementCreator psc=new PreparedStatementCreator() {

@Override
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement stmt=null;
try{
stmt=con.prepareStatement("insert into Emp values(?,?,?,?)");
stmt.setInt(1, e.getId());
stmt.setString(2, e.getName());
stmt.setString(3,e.getJob());
stmt.setInt(4, e.getSalary());
}catch(Exception ex){
System.out.println(ex);
}
return stmt;
}
};
template.update(psc);
}
public void update(final Emp e){
PreparedStatementCreator psc=new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement stmt=null;
try{
Scanner in=new Scanner(System.in);
System.out.println("Enter the id: ");
int id=in.nextInt();
stmt=con.prepareStatement
("update emp set name=?,job=?,salary=? where id=?");
stmt.setInt(4, id);
stmt.setString(1, e.getName());
stmt.setString(2, e.getJob());
stmt.setInt(3, e.getSalary());
}catch(Exception ex){
System.out.println(ex);
}
return stmt;
}
};
template.update(psc);
}
public void delete(final Emp e){
PreparedStatementCreator psc=new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement stmt=null;
try{
Scanner in=new Scanner(System.in);
System.out.println("Enter the id: ");
int id=in.nextInt();
stmt=con.prepareStatement("delete from emp where id=?");
stmt.setInt(1, id);

}catch(Exception ex){
System.out.println(ex);
}
return stmt;
}
};
template.update(psc);
}
}

InsertTest.java

package org.r4r;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class InsertTest {
public static void main(String[] args) {
ApplicationContext ctx=new 
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Obtaining dao from IOC container");
EmpTemplateDao dao=(EmpTemplateDao)ctx.getBean("dao");
System.out.println("Dao saved the Emp Object");
Emp e1=new Emp(101,"Amit","Engineer",25000);
Emp e2=new Emp(102,"ArshRaj","Developer",25000);
Emp e3=new Emp(103,"Dipu","Business Man",25000);
Emp e4=new Emp(104,"Amar","Marketing Manager",25000);
dao.save(e1);dao.save(e2);dao.save(e3);dao.save(e4);
System.out.println("Emp object Save");
}
}

UpdateTest.java

package org.r4r;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class UpdateTest {
public static void main(String[] args) {
ApplicationContext ctx=new 
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Obtaining dao from IOC container");
EmpTemplateDao dao=(EmpTemplateDao)ctx.getBean("dao");
System.out.println("Dao update the Emp Object");
Emp e=new Emp(222,"Mukund Singh","Software Engineer",55000);
dao.update(e);
System.out.println("Emp object Update");
}
}

DeleteTest.java

package org.r4r;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class DeleteTest {
public static void main(String[] args) {
ApplicationContext ctx=new 
ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Obtaining dao from IOC container");
EmpTemplateDao dao=(EmpTemplateDao)ctx.getBean("dao");
System.out.println("Deleting Emp Object");
Emp e=new Emp();
dao.delete(e);
System.out.println("Emp object delete");
}
}

SelectTest.java

package org.r4r;
import java.util.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class SelectTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ApplicationContext ctx=new 
	ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Obtaining dao from IOC container");
EmpTemplateDao dao=(EmpTemplateDao)ctx.getBean("dao");
System.out.println("Fetching Emp Objects");
List<Emp> list=dao.getAllEmp();
Iterator<Emp> itr=list.iterator();
System.out.println("Following objects are fetched.");
while(itr.hasNext()){
Emp e=itr.next();
System.out.println(e.getId()+"\t"+
e.getName()+"\t"+e.getJob()+"\t"+e.getSalary());
}
}
}

output:

InsertTest Result

Obtaining dao from IOC container
Dao saved the Emp Object
Emp object Save
UpdateTest Result:-

Obtaining dao from IOC container
Dao update the Emp Object
Enter the id: 
102
Emp object Update
DeleteTest Result:-
Obtaining dao from IOC container
Deleting Emp Object
Enter the id: 
102
Emp object delete
SelectTest Result:-

Obtaining dao from IOC container
Fetching Emp Objects
Following objects are fetched.
103	Amar	Marketing	25000
101	ArshRaj	Develo	25000
100	Amit	Engi	25000
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: