Update using JdbcTemplate

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 update record into data base using JdbcTempalte.
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 JdbcTemplate jdbcTemplate;
 private DataSource dataSource;
 
 public void insertDepartment(){
  String sql = "insert into departments(department_id, department_name, location_id) values (?,?,?)";
  jdbcTemplate.update(sql, new Object[]{280, "ABC", 1700});
 }
 
 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
 public DataSource getDataSource() {
  return dataSource;
 }
 @Autowired
 public void setDataSource(DataSource dataSource) {
  this.jdbcTemplate = new JdbcTemplate(dataSource);
 }
 
}

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 dao = context.getBean("departmentsDaoImpl", DepartmentsDaoImpl.class);
  
  dao.insertDepartment();
 }
}

Execute DepartmentApp.java file. Output is given below.

No comments:

Post a Comment