---
title: "Spring JDBC 调用 procedure"
author: "taink"
date: "2011-03-25T16:12:22Z"
tags: ["DAO", "JDBC", "Java", "SQL", "Spring"]
categories: ["企业架构"]
slug: "spring-jdbc-procedure"
draft: false
lastmod: "2011-03-25T16:12:22Z"
id: 975390
url: "https://taink.iteye.com/blog/975390"
---

import java.sql.CallableStatement;  
import java.sql.Connection;  
import java.sql.SQLException;  
import java.util.HashMap;  
import java.util.Map;  
import org.apache.commons.logging.Log;  
import org.springframework.dao.DataAccessException;  
import org.springframework.jdbc.core.CallableStatementCallback;  
import org.springframework.jdbc.core.CallableStatementCreator;  
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.qeweb.redants.dao.ISequenceDao;

public class SequenceDaoImpl extends SimpleJdbcDaoSupport implements  
        ISequenceDao {  
    Log log = org.apache.commons.logging.LogFactory  
            .getLog(SequenceDaoImpl.class);

    public String findSequence() {  
        String result = (String) getJdbcTemplate().execute(  
                new ProcCallableStatementCreator(),  
                new ProcCallableStatementCallBack());  
        log.info("new material Code:" + result);  
        return result;  
    }

    class ProcCallableStatementCreator implements CallableStatementCreator {  
        public CallableStatement createCallableStatement(Connection connection)  
                throws SQLException {  
            String proc\_name = "{call proc\_sequence(?)}";  
            CallableStatement callableStatement = connection  
                    .prepareCall(proc\_name);  
            callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR);  
            return callableStatement;  
        }  
    }

    class ProcCallableStatementCallBack implements CallableStatementCallback {  
        public Object doInCallableStatement(CallableStatement callableStatement)  
                throws SQLException, DataAccessException {  
            callableStatement.execute();  
            String seq = callableStatement.getString(1);  
            return seq;  
        }  
    }

}