Note: <context:annotation-config/>, <context:component-scan>, @Component and
@Autowired are explained under the "Points to ponder" section of the post
JbdcTemplate example
Below program is an example of how to get the data from the database using JdbcTemplateby passing a parameter.
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 Department_name given department_id public String getDepartmentName(int department_id){ String sql = "select department_name from departments where department_id = ?"; return jdbcTemplate.queryForObject(sql, new Object[] {department_id}, String.class); } 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.DepartmentApp.java
package com.ram.main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ram.dao.DepartmentsDaoImpl; public class DepartmentApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DepartmentsDaoImpl departments = context.getBean("departmentsDaoImpl", DepartmentsDaoImpl.class); System.out.println("Depatment name for department_id 30 is "+departments.getDepartmentName(30)); } }
No comments:
Post a Comment