Saturday, June 9, 2012

Spring : Working with JdbcTemplates

Spring provides JDBC Templates to access the data from the database with ease.Spring provides
org.springframework.jdbc.core.JdbcTemplate which basically take cares about all your
connection establishment to the database post query work eg closingh statement and connections.

Merits of spring templates as compared to conventinal Data access through JDBC
1. Connection Handling is done by Spring container you need not to worry about it,by this way
it reduced the singnificant amount of the coding.
2. Exception handling in conventional data access through JDBC is very cumbersome process,
here Spring take care of all these stuff by converting SQLException to the RuntimeException
3.ResultSetExtractor or RowMapper which allows result directl into the Object ResultSetExtractor
or a list of objects RowMapper.

Lets have a walk through how to work with JdbcTemplates

Step 1: Create your database as fallows,Here I am using MySQL as database.




Step 2: Create the Java Project and add Spring framework jar library to it.
Step 3: Create spring.xml as shown



 
 
 
 
  
  
  
  
  
  
 
 




Step 4:
Create Employee Model Class as shown
package com.example.model;

public class Employee {
 
 private int empid;
 private String empName;
 
 public Employee(int empid,String empName){
  this.empid = empid;
  this.empName = empName;
 }
 
 public int getEmpid() {
  return empid;
 }
 public void setEmpid(int empid) {
  this.empid = empid;
 }
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 

}

Step 5:Create the Data Access Object as fallows
package com.example.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.example.model.Employee;
@Component
public class EmpDaoImpl {
 
 @Autowired
 private DataSource dataSource;
 
 public void create(int empid, String employee_name) {
  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.update("INSERT INTO EMPLOYEE (empid, EMPLOYEE_NAME) VALUES(?,?)",
    new Object[] { empid, employee_name });
 }
 
 public DataSource getDataSource() {
  return dataSource;
 }

 public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
 }

}

Step 6: Create your main class as fallows to check whether all these things are in right shape.
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.example.dao.EmpDaoImpl;
import com.example.model.Employee;

public class Main {
 
 public static void main(String[] args) {
  
  ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
  EmpDaoImpl empdl = ctx.getBean("empDaoImpl",EmpDaoImpl.class);
  
  empdl.create(1, "Sharad Singh");  
  
  System.out.println("Data Inserted In Database");
 }

}

Output @ console:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Data Inserted In Database

Step 7:
Look into the database table whether data has been updated or not,Here you go......






Post a Comment