本站首页    管理页面    写新日志    退出


«November 2025»
1
2345678
9101112131415
16171819202122
23242526272829
30


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9743006
建立时间:2004年12月20日




[java语言]使用spring实现的占位存储过程的调用
原创空间,  软件技术,  电脑与网络

邢红瑞 发表于 2006/3/16 16:05:38

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.CallableStatementCallback;import org.springframework.jdbc.core.namedparam.NamedParameterUtils;import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;import org.springframework.core.CollectionFactory; import java.util.*;import java.sql.*; /** * Created by IntelliJ IDEA. * User: 邢红瑞 * Date: 2006-12-19 * Time: 16:18:34 * To change this template use File | Settings | File Templates. */public class CommonDBDao extends NamedParameterJdbcDaoSupport  {       private String procedure;     /**     * @return     */    public String getProcedure() {        return procedure;    }     public void setProcedure(String procedure) {        procedure = "{ call " + procedure + " } ";        this.procedure = procedure;    }     public List<Map> queryMaps(Map condMapForQuery) {        final ArrayList list = new ArrayList();        final Object[] sqlarray = NamedParameterUtils.buildValueArray(procedure, condMapForQuery);        String sqlstr = NamedParameterUtils.substituteNamedParameters(procedure, new MapSqlParameterSource(condMapForQuery));        final String sqlstr0 = sqlstr.replace("{", "{ ?=");        Object o = getJdbcTemplate().execute(sqlstr0, new CallableStatementCallback() {            public Object doInCallableStatement(CallableStatement arg0) throws SQLException, DataAccessException {                arg0.registerOutParameter(1, Types.INTEGER);                for (int i = 0; i < sqlarray.length; i++) {                    arg0.setObject(i + 2, sqlarray[i]);                }                ResultSet rs = arg0.executeQuery();                ListResultSetExtractor ls = new ListResultSetExtractor();                list.addAll((Collection) ls.extractData(rs));                return null;            }        });        return list;    }     public static Map CreateMap(int result) {        Map map = new HashMap();        map.put("result", new Integer(result));        return map;    }     private static class ListResultSetExtractor {         public Object extractData(ResultSet rs) throws SQLException {            List listOfRows = new ArrayList();            ResultSetMetaData rsmd = null;            int numberOfColumns = -1;            while (rs.next()) {                if (rsmd == null) {                    rsmd = rs.getMetaData();                    numberOfColumns = rsmd.getColumnCount();                }                Map mapOfColValues = CollectionFactory.createLinkedMapIfPossible(numberOfColumns);                for (int i = 1; i <= numberOfColumns; i++) {                    switch (rsmd.getColumnType(i)) {                        case java.sql.Types.TIMESTAMP:                            mapOfColValues.put(rsmd.getColumnName(i), rs.getTimestamp(i));                            break;                        default:                            mapOfColValues.put(rsmd.getColumnName(i), rs.getObject(i));                    }                }                listOfRows.add(mapOfColValues);            }            return listOfRows;        }    }}配置文件 <property name="procedure">           <value>             <![CDATA[pr_get_log_list(:forward,:id)  ]]></value></property>


阅读全文(4112) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.078 second(s), page refreshed 144812416 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号