Spring调用存储过程一个小记录
JAVA:
- public void testSpringPackage() {
- SpringStoredProce springStoredProce = new SpringStoredProce(this
- .getJdbcTemplate());
- List< Map > resultList = springStoredProce.execute(0, 10,
- " and t.seed_name_cn like '%竹%' ");
- for (int i = 0; i < resultList.size(); i++) {
- + "\t password=" + password);
- }
- }
JAVA:
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.dao.DataAccessException;
- import org.springframework.jdbc.core.CallableStatementCallback;
- import org.springframework.jdbc.core.CallableStatementCreator;
- import org.springframework.jdbc.core.JdbcTemplate;
- import com.rizon.eiss.util.StringUtils;
- public class SpringStoredProce {
- private JdbcTemplate jdbcTemplate;
- public SpringStoredProce(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
- List< Map > resultList = null;
- try {
- resultList = (List< Map >) this.jdbcTemplate.execute(
- new ProcCallableStatementCreator(start_no, end_no, filter),
- new ProcCallableStatementCallback());
- } catch (DataAccessException e) {
- "execute method error : DataAccessException "
- + e.getMessage());
- }
- return resultList;
- }
- /**
- * Create a callable statement in this connection.
- */
- private class ProcCallableStatementCreator implements
- CallableStatementCreator {
- private long start_no;
- private long end_no;
- private String filter;
- public ProcCallableStatementCreator(long start_no, long end_no,
- this.start_no = start_no;
- this.end_no = end_no;
- this.filter = filter;
- }
- CallableStatement cs = null;
- try {
- cs = conn.prepareCall("{call TREE_PACKAGE.TREE_FIND(?,?,?,?)}");
- cs.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
- cs.setLong(2, start_no);
- cs.setLong(3, end_no);
- cs.setString(4, filter);
- "createCallableStatement method Error : SQLException "
- + e.getMessage());
- }
- return cs;
- }
- }
- private class ProcCallableStatementCallback implements
- CallableStatementCallback {
- public ProcCallableStatementCallback() {
- }
- List< Map > resultsMap = new ArrayList< Map >();
- try {
- cs.execute();
- while (rs.next()) {
- Map< String, Object > rowMap = new HashMap< String, Object >();
- rowMap.put("t_id", rs.getLong("t_id"));
- rowMap.put("status", StringUtils.Trim(rs.getString("status")));
- rowMap.put("tree_name", StringUtils.Trim(rs.getString("tree_name")));
- rowMap.put("seed_name_cn", StringUtils.Trim(rs.getString("seed_name_cn")));
- rowMap.put("seed_name_en", StringUtils.Trim(rs.getString("seed_name_en")));
- rowMap.put("seed_comment", StringUtils.Trim(rs.getString("seed_comment")));
- rowMap.put("gid", StringUtils.Trim(rs.getString("gid")));
- rowMap.put("g_name_cn", StringUtils.Trim(rs.getString("g_name_cn")));
- rowMap.put("g_name_en", StringUtils.Trim(rs.getString("g_name_en")));
- rowMap.put("g_class", StringUtils.Trim(rs.getString("g_class")));
- rowMap.put("tagsname", StringUtils.Trim(rs.getString("tagsname")));
- rowMap.put("tagsids", StringUtils.Trim(rs.getString("tagsids")));
- rowMap.put("tags_des", StringUtils.Trim(rs.getString("tags_des")));
- resultsMap.add(rowMap);
- }
- rs.close();
- "doInCallableStatement method error : SQLException "
- + e.getMessage());
- }
- return resultsMap;
- }
- }
- }