This example performs the database operations insert/delete/select/update on the table named Person using Spring JdbcDaoSupport.
Database: MySQL
Table Structure:
Field | Type | Null | Key | Default | Extra |
id | varchar(20) | NO | PRI | ||
name | varchar(50) | YES | (NULL) | ||
age | int(11) | YES | (NULL) | ||
place | varchar(50) | YES | (NULL) |
Jar files those should be present in classpath
spring-jdbc.jar
spring.jar
commons-logging.jar
mysql-connector-java-5.0.4-bin.jar
Java files
Person.java
Bean, which holds the person information.
PersonDao.java
Interface, which contains the base methods for database operations.
PersonDaoImpl.java
Implements the interface PersonDao and extends JdbcDaoSupport.
The DataSource object is injected to this bean from PersonDao.xml.
PersonBO.java
From this class the dao methods are invoked.
PersonDao.xml
This contains the bean definitions required for dao call.
package rhymes.springdao.bean;
public class Person {
private String id;
private String name;
private Integer age;
private String place;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String toString(){
StringBuffer person = new StringBuffer();
person.append("\nId " + id);
person.append("\nName " + name);
person.append("\nPlace " + place);
person.append("\nAge " + age);
return person.toString();
}
}
package rhymes.springdao;
import java.util.List;
import rhymes.springdao.bean.Person;
public interface PersonDao{
public int insert(Person person);
public int update(Person person);
public int delete(Person person);
public Person findByPrimaryKey(String id);
public List get();
}
package rhymes.springdao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import rhymes.springdao.PersonDao;
import rhymes.springdao.bean.Person;
public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao{
public int delete(Person person) {
String sql = "delete from Person where id = ?";
Object[] args = {person.getId()};
int status=this.getJdbcTemplate().update(sql, args);
return status;
}
public Person findByPrimaryKey(String id) {
String sql = "select id,name,age,place from Person where id = ?";
Object[] args = {id};
List personList = this.getJdbcTemplate().query(sql, args, new ParameterizedRowMapper<Person>(){
public Person mapRow(ResultSet rs, int rowNum)
throws SQLException {
Person person = new Person();
person.setAge(rs.getInt("age"));
person.setId(rs.getString("id"));
person.setName(rs.getString("name"));
person.setPlace(rs.getString("place"));
return person;
}
});
Person person = null;
for(int i=0;i<personList.size();i++){
person=(Person)personList.get(i);
}
return person;
}
public int insert(Person person) {
String sql = "insert into Person (id,name,age,place) values(?,?,?,?)";
Object[] args = {person.getId(),person.getName(),person.getAge(),person.getPlace()};
int status=this.getJdbcTemplate().update(sql, args);
return status;
}
public int update(Person person) {
String sql = "update Person set name= ?,age= ?,place= ? where id = ?";
Object[] args = {person.getName(),person.getAge(),person.getPlace(),person.getId()};
int status=this.getJdbcTemplate().update(sql, args);
return status;
}
public List get() {
String sql = "select id,name,age,place from Person";
List personList = this.getJdbcTemplate().query(sql, new ParameterizedRowMapper<Person>(){
public Person mapRow(ResultSet rs, int rowNum)
throws SQLException {
Person person = new Person();
person.setAge(rs.getInt("age"));
person.setId(rs.getString("id"));
person.setName(rs.getString("name"));
person.setPlace(rs.getString("place"));
return person;
}
});
return personList;
}
}
package rhymes.springbo;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import rhymes.springdao.PersonDao;
import rhymes.springdao.bean.Person;
public class PersonBO {
private PersonDao personDao;
public static void main(String[] args) throws NumberFormatException, IOException {
PersonBO personBO = new PersonBO();
BeanFactory BeanFactory = new ClassPathXmlApplicationContext("PersonDao.xml");
personBO.setPersonDao((PersonDao)BeanFactory.getBean("personDao"));
//personBO.insert();
//personBO.update();
//personBO.find();
//personBO.get();
//personBO.delete();
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void insert(){
Person person = new Person();
person.setAge(new Integer(26));
person.setId("4");
person.setName("Suraj");
person.setPlace("Kochi");
int status = personDao.insert(person);
System.out.println("Status: " +status);
}
public void delete() {
Person person = new Person();
person.setId("3");
int status = personDao.delete(person);
System.out.println("Status: " +status);
}
public void update(){
Person person = new Person();
person.setAge(new Integer(28));
person.setId("3");
person.setName("Sandeep");
person.setPlace("jaipur");
int status = personDao.update(person);
System.out.println("Status: " +status);
}
public void find() {
Person person=personDao.findByPrimaryKey("3");
System.out.println(person);
}
public void get() {
List<Person> personList = personDao.get();
for(int i=0;i<personList.size();i++){
Person person=(Person)personList.get(i);
System.out.println(person);
}
}
}
<?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="personDao" class="rhymes.springdao.impl.PersonDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/person"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</bean>
</beans>
1 comment:
Hi Suraj,
Thanks for a very informative post.
Vinu
Post a Comment