在执行查询之前和之后,我们需要编写很多代码,例如创建连接,语句,关闭结果集,连接等。
我们需要在数据库逻辑上执行异常处理代码。
我们需要处理交易。
将所有这些代码从一个数据库逻辑重复到另一个数据库逻辑是一项耗时的工作。
JdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcTemplate
SimpleJdbcInsert和SimpleJdbcCall
方法 | 说明 |
public int update(String query) | 用于插入,更新和删除记录。 |
public int update(String query,Object ... args) | 用于通过使用给定参数的PreparedStatement插入,更新和删除记录。 |
public void execute(String query) | 用于执行DDL查询。 |
public T execute(String sql, PreparedStatementCallback action) | 通过使用PreparedStatement回调执行查询。 |
public T query(String sql, ResultSetExtractor rse) | 用于使用ResultSetExtractor获取记录。 |
public List query(String sql, RowMapper rse) | 用于使用RowMapper获取记录。 |
create table employee(
id number(10),
name varchar2(100),
salary number(10)
);
package com.lidihuo;
public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
}
package com.lidihuo;
import org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
String query="insert into employee values(
'"+e.getId()+"','"+e.getName()+"','"+e.getSalary()+"')";
return jdbcTemplate.update(query);
}
public int updateEmployee(Employee e){
String query="update employee set
name='"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' ";
return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee e){
String query="delete from employee where id='"+e.getId()+"' ";
return jdbcTemplate.update(query);
}
}
<?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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="edao" class="com.lidihuo.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
package com.lidihuo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
int status=dao.saveEmployee(new Employee(102,"Amit",35000));
System.out.println(status);
/*int status=dao.updateEmployee(new Employee(102,"Sonoo",15000));
System.out.println(status);
*/
/*Employee e=new Employee();
e.setId(102);
int status=dao.deleteEmployee(e);
System.out.println(status);*/
}
}