JbdcTemplate example

Points to ponder:
  • <context:annotation-config/> : It is used to activate annotations in beans already registered in the application context.
  • <context:component-scan/> : It scans packages to find and register beans within the application context.
  • @Component : It is one of several stereotype annotations. @Component is a general-purpose stereotype annotation indicating that the class is a Spring component
  • @Autowired : @Autowired indicates that spring should inject dependency, based on its type or based on its name if used with the @Qualifier annotation.
Below program is an example of how to connect to data base using JdbcTemplate.
File: com.ram.model.Departments.java
package com.ram.model;

public class Departments {
 private int department_id;
 private String department_name;
 private int manager_id;
 private int location_id;
 
 public int getDepartment_id() {
  return department_id;
 }
 public void setDepartment_id(int department_id) {
  this.department_id = department_id;
 }
 public String getDepartment_name() {
  return department_name;
 }
 public void setDepartment_name(String department_name) {
  this.department_name = department_name;
 }
 public int getManager_id() {
  return manager_id;
 }
 public void setManager_id(int manager_id) {
  this.manager_id = manager_id;
 }
 public int getLocation_id() {
  return location_id;
 }
 public void setLocation_id(int location_id) {
  this.location_id = location_id;
 }
}

Next create applicationContext.xml file under src. The file is given below.
<?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-3.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
 <context:annotation-config/>
 
 <context:component-scan base-package="com.ram.dao"></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="dbc:oracle:thin:@localhost:1521:xe"></property>
  <property name="username" value="hr"></property>
  <property name="password" value="hr"></property>
 </bean>
 
</beans>
File: com.ram.dao.DepartmentsDaoImpl.java
package com.ram.dao;

import javax.sql.DataSource;

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

@Component
public class DepartmentsDaoImpl {
 private DataSource dataSource;
 private JdbcTemplate jdbcTemplate;
 
 //Method to get the number of rows in departments table
 public int getDepartmentsCount(){
  String sql = "select count(*) from departments";
  return jdbcTemplate.queryForInt(sql);
 }
 
 public DataSource getDataSource() {
  return dataSource;
 }
 
 @Autowired
 public void setDataSource(DataSource dataSource) {
  this.jdbcTemplate = new JdbcTemplate(dataSource);
 }
 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 
}

File: com.ram.main.DepartmentsDao.java
package com.ram.main;

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

import com.ram.dao.DepartmentsDaoImpl;

public class DepartmentsDao {
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  DepartmentsDaoImpl departments = context.getBean("departmentsDaoImpl", DepartmentsDaoImpl.class);
  System.out.println("Total number of rows in Departments table = "+departments.getDepartmentsCount());
 }
}

Execute DepartmentsDao.java file. Output is given below.

No comments:

Post a Comment