Spring Jdbc Template

JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers.

It is the central class in the Spring JDBC support classes. It takes care of creation and release of resources such as creating and closing of connection object etc. So it will not lead to any problem if you forget to close the connection.

It handles the exception and provides the informative exception messages by the help of excepion classes defined in the org.springframework.dao package.

We can perform all the database operations by the help of JdbcTemplate class such as insertion, updation, deletion and retrieval of the data from the database.

Example With JdbcTemplate


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

  public void insert(Student student){
  String sql = "INSERT INTO Student " +
  "(ROLL_NO, NAME, AGE) VALUES (?, ?, ?)";
  jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.update(sql, new Object[] 
{ student.getRollNo(),
   student.getName(),student.getAge()  
	});
 
	}

Example With JdbcDaoSupport

JdbcDaoSupport, set the datasource and JdbcTemplate in your class is no longer required, you just need to inject the correct datasource into JdbcCustomerDAO. And you can get the JdbcTemplate by using a getJdbcTemplate() method.

public class JdbcStudentDAO extends JdbcDaoSupport
 implements StudentDAO
{
	   //no need to set datasource here
   public void insert(Student student){
 
   String sql = "INSERT INTO STUDENT " +
   "(ROLL_NO, NAME, AGE) VALUES (?, ?, ?)";
  getJdbcTemplate().update(sql, new Object[] 
 { 
  student.getRollNo(),
  student.getName(),student.getAge()  
 });
 
 }


<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="dataSource"  
class="org.springframework.jdbc
.datasource.DriverManagerDataSource">
<property name="driverClassName"
 value="com.mysql.jdbc.Driver" />
<property name="url" 
value="jdbc:mysql://localhost:3306/javasafaridb" />
<property 
name="username" value="root" />
<property name="password" value="password" /></bean>
</beans>
<beans> 
 <bean id="studentDAO" 
class="com.javasafari.student.dao.
impl.JdbcStudentDAO">
<property
 name="dataSource" ref="dataSource" />
</bean>
</beans>

NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.

Example


private NamedParameterJdbcTemplate 
namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.namedParameterJdbcTemplate =
 new NamedParameterJdbcTemplate(dataSource);
}
public int countOfActorsByFirstName(String firstName) {
String sql = "select count(*) from Student where 
first_name = :first_name";
SqlParameterSource namedParameters = 
new MapSqlParameterSource("first_name", firstName);
return this.namedParameterJdbcTemplate.queryForObject
(sql, namedParameters, Integer.class);
}


SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.

Example

public class JdbcStudentDao implements StudentDao {
private JdbcTemplate jdbcTemplate;
private SimpleJdbcInsert insertStudent;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.insertStudent = new SimpleJdbcInsert(dataSource).
withTableName("STUDENT_t");
}
public void add(Student student) {
Map parameters = new HashMap(3);
parameters.put("rollno", student.getId());
parameters.put("first_name", student.getFirstName());
parameters.put("last_name", student.getLastName());
insertStudent.execute(parameters);
}
// ... additional methods
}