English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring SimpleJdbcTemplate 예제

Spring 3 JDBC는 SimpleJdbcTemplate 클래스를 통해 Java를 지원합니다. 5기능 var-args(가변 파라미터)와 자동装箱

SimpleJdbcTemplate 클래스는 JdbcTemplate 클래스를 포장하고, 파라미터를 전달할 수 있는 update 메서드를 제공합니다.

SimpleJdbcTemplate 클래스의 update 메서드 문법

int update(String sql, Object... 参数)

파라미터화된 쿼리에서 정의된 순서대로 업데이트 메서드에 파라미터 값을 전달해야 합니다.


SimpleJdbcTemplate 클래스의 예제

Oracle에서 이미 설치되어 있다고 가정합니다.10g 데이터베이스에 다음 테이블이 생성되었습니다.

employee 테이블을 생성합니다.
id number(10),
name varchar2(100),
salary number(10)
);

Employee.java

이 클래스는 다음을 포함합니다3이 클래스는 생성자, setter 및 getter를 포함한 속성을 가지고 있습니다.

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

이 클래스는 SimpleJdbcTemplate 속성과 메서드를 포함하고 있습니다. 이 경우, update 메서드는 해당 ID의 이름만 업데이트합니다. 이름과 급여를 동시에 업데이트하려면, update 메서드의 위의 두 줄 코드를 주석 처리하고 아래 제공된 두 줄 코드를 주석 해제하세요.

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 데이터베이스에 대한 정보를 포함하는 용도로 사용됩니다. 예를 들어, 드라이버 클래스 이름, 연결 URL, 사용자 이름 및 비밀번호.

DriverManagerDataSource형의 SimpleJdbcTemplate 클래스에는 이름이 datasource 의 속성을 사용합니다. 따라서 SimpleJdbcTemplate 클래스에서 DriverManagerDataSource 객체의 참조를 데이터 소스 속성에 제공해야 합니다.

EmployeeDao 클래스에서 SimpleJdbcTemplate 객체를 사용하므로, 그것을 생성자를 통해 전달하지만, setter 메서드를 사용할 수도 있습니다.

<?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.w3codebox.EmpDao">
<constructor-arg>
<ref bean="jtemplate"/>
</constructor-arg>
</bean>
</beans>

SimpleTest.java

이러한 클래스는 applicationContext.xml 파일에서 Bean을 가져와 EmpDao 클래스의 업데이트 메서드를 호출합니다.

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);
}
}