带参数模版方法实现机制

近日,在研究 Spring ,看到模板方法有这样一段代码:

 一时没有搞明白,经过仔细研究 发现可以用内部类来实现,而Spring

时用接口来实现的。以下时我写的内部类的实现方法:


 

mysql:

create   database  school;

use  school;

create   table  student
(
    stu_id 
int   not   null   primary   key ,
    stu_name 
varchar ( 20 not   null ,
    stu_age 
int
);

select   *   from  student;

 


 

 

applicationContext.xml:

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans >
    
< bean  id ="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
< property  name ="url" >
            
< value > jdbc:mysql:///school </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
    
</ bean >
</ beans >

DriverManager.java
package  test;

import  javax.sql.DataSource;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  DriverManager  {
    
public DataSource getDataSource() {
        ApplicationContext context 
= new ClassPathXmlApplicationContext(
                
"applicationContext.xml");

        javax.sql.DataSource dataSource 
= (DataSource) context
                .getBean(
"dataSource");

        
return dataSource;

    }


    
public static void main(String[] args) {
        javax.sql.DataSource d 
= new DriverManager().getDataSource();

        System.out.println(d);
    }

}

JdbcTemplate.java
package  test;

import  java.sql.PreparedStatement;
import  java.sql.SQLException;

import  javax.sql.DataSource;

public   class  JdbcTemplate  {
    
private DataSource dataSource;

    
public JdbcTemplate(DataSource dataSource) {
        
super();
        
this.dataSource = dataSource;
    }


    
static class PreparedStatementSetter {
        
protected java.sql.PreparedStatement pstmt;

        
public void setValues(PreparedStatement pstmt) throws SQLException {
            
this.pstmt = pstmt;
        }

    }


    
public void insert(String sql, PreparedStatementSetter pss) {
        
try {
            java.sql.Connection con 
= dataSource.getConnection();
            java.sql.PreparedStatement pstmt 
= con.prepareStatement(sql);

            
try {
                pss.setValues(pstmt);
            }
 catch (Exception e) {
                
// TODO 自动生成 catch 块
                e.printStackTrace();
            }

            pstmt.executeUpdate();

            pstmt.close();
            con.close();

        }
 catch (Exception e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }


    }


    
public static void main(String[] args) {

        DriverManager d 
= new DriverManager();
        javax.sql.DataSource dataSource 
= d.getDataSource();

        JdbcTemplate template 
= new JdbcTemplate(dataSource);

        String sql 
= "insert into student values (?,?,?)";

        template.insert(sql, 
new PreparedStatementSetter() {
            
public void setValues(PreparedStatement pstmt) {
                
try {
                    pstmt.setInt(
11112);
                    pstmt.setString(
2"aaaaaaaaaa");
                    pstmt.setInt(
319);
                }
 catch (Exception e) {
                    
// TODO 自动生成 catch 块
                    e.printStackTrace();
                }


            }

        }
);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值