`

Spring JDBC 调用 procedure

    博客分类:
  • J2EE
阅读更多

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

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics