English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring 3 JDBC supports Java with the help of SimpleJdbcTemplate class 5Function var-args (variable parameters) and auto-boxing.
SimpleJdbcTemplate class wraps the JdbcTemplate class and provides an update method that can pass an arbitrary number of parameters.
int update(String sql, Object... parameters)
We should pass the parameter values in the order defined in the parameterized query in the update method.
We assume that you have already installed Oracle10the following table was created in the G database.
create table employee( id number(10) name varchar2(100), salary number(10) );
Employee.java
This class includes3a class with a constructor, setter, and getter properties.
package com.w3codebox; public class Employee { private int id; private String name; private float salary; //no-arg and parameterized constructors //getters and setters }
EmployeeDao.java
It contains a property SimpleJdbcTemplate and a method update. In this case, the update method will only update the name corresponding to the ID. If you want to update both name and salary at the same time, comment out the two lines above the update method and uncomment the two lines given below.
package com.w3codebox; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; public class EmpDao { SimpleJdbcTemplate template; public EmpDao(SimpleJdbcTemplate template) { this.template = template; } public int update (Emp e){ String query="update employee set name=? where id=?"; return template.update(query, e.getName(), e.getId()); //String query="update employee set name=?,salary=? where id=?"; //return template.update(query, e.getName(), e.getSalary(), e.getId()); } }
applicationContext.xml
DriverManagerDataSource used to include information about the database, such as the driver class name, connection URL, username, and password.
The SimpleJdbcTemplate class of the DriverManagerDataSource type has a method named
datasource
properties. Therefore, we need to provide a reference to the DriverManagerDataSource object for the data source property in the SimpleJdbcTemplate class.
Here, we use the SimpleJdbcTemplate object in the EmployeeDao class, so we pass it through the constructor, but you can also use the setter method.
<?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:p="http://www.springframework.org/schema/p xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="oracle" /> </bean> <bean id="jtemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg ref="ds"></constructor-arg> </bean> <bean id="edao" class="com.w"3codebox.EmpDao"> <constructor-arg> <ref bean="jtemplate"/> </constructor-arg> </bean> </beans>
SimpleTest.java
This class retrieves a Bean from the applicationContext.xml file and calls the update method of the EmpDao class.
package com.w3codebox; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class SimpleTest { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); EmpDao dao=(EmpDao)factory.getBean("edao"); int status=dao.update(new Emp(23,"Tarun",35000)); System.out.println(status); } }