JdbcTemplate中的exectue和queryForList方法的性能对比

@Autowired
JdbcTemplate jdbcParam;

pstm =
                jdbcParam.getDataSource()
                    .getConnection()
                    .prepareStatement(" SELECT T.ID, T.EPARCHY_CODE FROM  TD_B_SPECIALID_HOME T WHERE T.ID_TYPE = 2 ");
            result = pstm.executeQuery();
            long acctId;
            String eparchyCode;
            while (result.next())
            {
                acctId = result.getLong("ID");
                eparchyCode = result.getString("EPARCHY_CODE");
                specialAcctIdWithEparchyCodeMap.put(acctId, eparchyCode);
            }
        }
        catch (Exception e)
        {
            logger.error("run initSpecialAcctIdWithEparchyCodeMap error.", e);
        }
        finally
        {
            try
            {
                if (!result.isClosed())
                {
                    result.close();
                }
            }
            catch (SQLException e)
            {
                logger.error("Can not close resultset conn.", e);
            }
            finally
            {
                try
                {
                    if (!pstm.isClosed())
                    {
                        pstm.close();
                    }
                }
                catch (SQLException e)
                {
                    logger.error("Can not close preparedstatment conn.", e);
                }
            }
        }

以上代码:需要打开preparedStatement和ResultSet连接,影响性能

而用jdbcParam.queryForList(sql,new Object[]{},Integer.class);

不需要做打开连接关闭连接动作,直接由spring容器管理,性能提升不少。

posted @ 2014-12-15 16:05  ppjj  阅读(669)  评论(0编辑  收藏  举报