Sunday, April 29, 2012

Spring JDBCTemplate

We'll use the JdbcTemplate class provided by Spring to implement a DAO method. We'll see how using the template class makes the DAO methods simple, and how it takes care of much of the boilerplate code we'd have to write otherwise.
Open Declaration org.springframework.jdbc.core.JdbcTemplate

This is the central class in the JDBC core package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving application code to provide SQL and extract results. This class executes SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package.
Code using this class need only implement callback interfaces, giving them a clearly defined contract. The PreparedStatementCreator callback interface creates a prepared statement given a Connection, providing SQL and any necessary parameters. The ResultSetExtractor interface extracts values from a ResultSet. See also PreparedStatementSetter and RowMapper for two popular alternative callback interfaces.
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
Because this class is parameterizable by the callback interfaces and the SQLExceptionTranslator interface, there should be no need to subclass it.
All SQL operations performed by this class are logged at debug level, using "org.springframework.jdbc.core.JdbcTemplate" as log category.


************************************************************************
package com.venkat;


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


import com.venkat.JdbcDao.JdbcDaoImpl;
import com.venkat.model.Login;


public class JDBCDemo {


/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception  {
// TODO Auto-generated method stub

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 JdbcDaoImpl dao=context.getBean("jdbcDaoImpl",JdbcDaoImpl.class);

//  Login login=dao.getLogin("venkat");

//System.out.println(login.getPassword());
 System.out.println(dao.getLoginCount());
}


}
************************************************************************
package com.venkat.JdbcDao;


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


import javax.sql.DataSource;


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


import com.venkat.model.Login;
@Component
public class JdbcDaoImpl {
private DataSource datasource;
private JdbcTemplate jdbcTemplate;



public DataSource getDatasource() {
return datasource;
}




public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}




public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}


@Autowired
public void setDatasource(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}


public int getLoginCount()
{
String sql="select count(*) from LOGIN";
//jdbcTemplate.setDataSource(getDatasource());
return jdbcTemplate.queryForInt(sql);

}
public Login getLogin(String userid) throws Exception
{
Connection con=datasource.getConnection();
PreparedStatement ps=con.prepareStatement("Select * from LOGIN where userid=?");
ps.setString(1, userid);

Login l=null;
ResultSet rs=ps.executeQuery();

if(rs.next())
{
l=new Login(userid, rs.getString("password"));
}
rs.close();
ps.close();
return l;




}
}
****************************************************************************
package com.venkat.model;


public class Login {

private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
public Login(String userid, String password) {
super();
this.userid = userid;
this.password = password;
}
 
}
*************************************************************************
<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<context:annotation-config/>


<context:component-scan base-package="com.venkat"></context:component-scan>






<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<property name="username" value="venkat"></property>
<property name="password" value="venkat"></property>
</bean></beans>

No comments:

Post a Comment