Spring教程

Spring JdbcTemplate中PreparedStatement的示例

借助JdbcTemplate类的 execute()方法,我们可以使用Spring JdbcTemplate执行参数化查询。要使用参数化查询,我们在execute方法中传递 PreparedStatementCallback 的实例。

execute方法的语法以使用参数化查询

public T execute(String sql,PreparedStatementCallback<T>);

PreparedStatementCallback接口

它处理输入参数和输出结果。在这种情况下,您不必关心单引号和双引号。
PreparedStatementCallback接口的方法
它只有一个方法doInPreparedStatement。该方法的语法如下:
public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException

在Spring中使用PreparedStatement的示例

我们假设您已在Oracle10g数据库中创建了下表。
create table employee(
id number(10),
name varchar2(100),
salary number(10)
);
Employee.java
此类包含3个带有构造函数,setter和getter的属性。
package com.lidihuo;
public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
}
EmployeeDao.java
它包含一个属性jdbcTemplate和一个方法saveEmployeeByPreparedStatement。您必须了解匿名类的概念才能了解该方法的代码。
package com.lidihuo;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}
public boolean saveEmployeeByPreparedStatement(final Employee e){
    String query="insert into employee values(?,?,?)";
    return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){
    @Override
    public boolean doInPreparedStatement(PreparedStatement ps)
            throws SQLException, DataAccessException {
            
        ps.setInt(1,e.getId());
        ps.setString(2,e.getName());
        ps.setfloat(3,e.getSalary());
            
        return ps.execute();
            
    }
    });
}
}
applicationContext.xml
DriverManagerDataSource 用于包含有关数据库的信息,例如驱动程序类名称,连接URL,用户名和密码。
DriverManagerDataSource类型的JdbcTemplate类中有一个名为 datasource 的属性。因此,我们需要在JdbcTemplate类中为数据源属性提供DriverManagerDataSource对象的引用。
在这里,我们在EmployeeDao类中使用了JdbcTemplate对象,因此我们通过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="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>
Test.java
此类从applicationContext.xml文件获取Bean,并调用saveEmployeeByPreparedStatement()方法。
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");
    dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000));
}
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4