转自:Spring 框架 执行SQL getJdbcTemplate().query update 用法2010-04-14 13:56
第二部分:转自: JdbcTemplate中的封装了很多实用的方法。 首先来介绍一下用JdbcTemplate来查询数据的方法。 1.返回指定对象类型的List (query方法)Java代码 Java代码 public List getProductModle(String value) throws DataAccessException { List resultList = new ArrayList(); List args = new ArrayList(); String sql = "select F_CPUT_MOD_ID as id ,F_CPUT_MOD_NAME as name from CPUT_MOD_T"+ " WHERE F_MARKET_LOCA_ID = ? order by F_CPUT_MOD_ID"; if(StringUtils.isNotEmpty(value)){ args.add(value); } resultList = this.getJdbcTemplate().query(sql,args.toArray(), new ProductModleRowMapper()); return resultList; } /** * @author rockjava * @description 封装产品型号结果集 */ private class ProductModleRowMapper implements RowMapper{ public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { ProductModleVO vo = new ProductModleVO(); vo.setProModleId((String)rs.getString("id")); vo.setProModleName((String)rs.getString("name")); return vo; } }public List getProductModle(String value) throws DataAccessException { List resultList = new ArrayList(); List args = new ArrayList(); String sql = "select F_CPUT_MOD_ID as id ,F_CPUT_MOD_NAME as name from CPUT_MOD_T"+ " WHERE F_MARKET_LOCA_ID = ? order by F_CPUT_MOD_ID"; if(StringUtils.isNotEmpty(value)){ args.add(value); } resultList = this.getJdbcTemplate().query(sql,args.toArray(), new ProductModleRowMapper()); return resultList; } /** * @author rockjava * @description 封装产品型号结果集 */ private class ProductModleRowMapper implements RowMapper{ public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { ProductModleVO vo = new ProductModleVO(); vo.setProModleId((String)rs.getString("id")); vo.setProModleName((String)rs.getString("name")); return vo; } } 这里实现了RowMapper类的mapRow方法,用来把查询到的结果集用指定的类来封装,最后返回一个List,List中装着 我们自定的值对象。 2.返回指定类型的结果 (queryForObject方法)Java代码 public String getCurrentPhase(String arg) throws DataAccessException { String currentPhase = ""; String sql="select F_CURR_YEAR from COMPETITION_BASE_T where F_COMPETITION_ID=?"; Object[] o ={arg}; try { currentPhase = (String)this.getJdbcTemplate().queryForObject(sql,o, String.class); } catch (Exception e) { currentPhase = ""; e.printStackTrace(); } return currentPhase; } public String getCurrentPhase(String arg) throws DataAccessException { String currentPhase = ""; String sql="select F_CURR_YEAR from COMPETITION_BASE_T where F_COMPETITION_ID=?"; Object[] o ={arg}; try { currentPhase = (String)this.getJdbcTemplate().queryForObject(sql,o, String.class); } catch (Exception e) { currentPhase = ""; e.printStackTrace(); } return currentPhase; } 在这个例子中,用到了queryForObject这个方法,其中第一个参数是要执行的sql,第二个参数是Object数组类型(其中装的是sql脚本用到 的参数),第三个参数就是要制定返回结果的类型(这里我定义的是String类型) 3.查询结果返回Int类型 (queryForInt方法) Java代码 public boolean doCheckCompete(Map args) throws DataAccessException { ....省略代码 /**sql脚本用到的参数集合*/ Object[] args = new Object[]{ companyId, competitionId, marketType, martID, currentPhase }; StringBuffer sql = new StringBuffer(); sql.append("SELECT COUNT ( * ) "); sql.append("FROM BEFOR_ORDER_T t "); sql.append("WHERE T.F_COMPANY_ID = ? "); sql.append(" and T.F_COMPETITION_ID=? "); sql.append(" and T.F_MARKET_LOCA_ID=? "); sql.append(" and T.F_MARKET_SCOPE_ID=? "); sql.append(" and T.F_CURRENT_PHASE = ? "); int i=0; i = this.getJdbcTemplate().queryForInt(sql.toString(), args); if(i>0){ return true; } return false; } public boolean doCheckCompete(Map args) throws DataAccessException { ....省略代码 /**sql脚本用到的参数集合*/ Object[] args = new Object[]{ companyId, competitionId, marketType, martID, currentPhase }; StringBuffer sql = new StringBuffer(); sql.append("SELECT COUNT ( * ) "); sql.append("FROM BEFOR_ORDER_T t "); sql.append("WHERE T.F_COMPANY_ID = ? "); sql.append(" and T.F_COMPETITION_ID=? "); sql.append(" and T.F_MARKET_LOCA_ID=? "); sql.append(" and T.F_MARKET_SCOPE_ID=? "); sql.append(" and T.F_CURRENT_PHASE = ? "); int i=0; i = this.getJdbcTemplate().queryForInt(sql.toString(), args); if(i>0){ return true; } return false; } 这里用的是queryForInt方法返回int类型。 4.查询结果返回Map类型 (queryForMap方法) Java代码 String sql = "select T.F_COMPETE_ORDER_STATE ,T.F_GENERATED_ORDERS " + "from COMPETITION_BASE_T t " + "where T.F_COMPETITION_ID = ?"; Map state = (Map)this.getJdbcTemplate().queryForMap(sql,new Object[]{args}); String compete_order_state = state.get("F_COMPETE_ORDER_STATE").toString(); String generated_orders = state.get("F_GENERATED_ORDERS").toString(); String sql = "select T.F_COMPETE_ORDER_STATE ,T.F_GENERATED_ORDERS " + "from COMPETITION_BASE_T t " + "where T.F_COMPETITION_ID = ?"; Map state = (Map)this.getJdbcTemplate().queryForMap(sql,new Object[]{args}); String compete_order_state = state.get("F_COMPETE_ORDER_STATE").toString(); String generated_orders = state.get("F_GENERATED_ORDERS").toString(); 返回后的map中的键值对应的是select语句中的字段名字。 5.查询结果集直接返回list (queryForList方法)Java代码 List list = new ArrayList(); String sql="select T.F_OFF_PERIOD,T.F_PROBABILITY from ORDER_OFF_PERIOD_T t "+ "where T.F_COMPETITION_ID=?"; Object[] args ={arg}; try { list = this.getJdbcTemplate().queryForList(sql, args); } catch (Exception e) { e.printStackTrace(); } ...代码省略 String a = (String)(((Map) list.get(0)).get("F_PROBABILITY")); String b = (String)(((Map) list.get(1)).get("F_PROBABILITY")); String c = (String)(((Map) list.get(2)).get("F_PROBABILITY")); List list = new ArrayList(); String sql="select T.F_OFF_PERIOD,T.F_PROBABILITY from ORDER_OFF_PERIOD_T t "+ "where T.F_COMPETITION_ID=?"; Object[] args ={arg}; try { list = this.getJdbcTemplate().queryForList(sql, args); } catch (Exception e) { e.printStackTrace(); } ...代码省略 String a = (String)(((Map) list.get(0)).get("F_PROBABILITY")); String b = (String)(((Map) list.get(1)).get("F_PROBABILITY")); String c = (String)(((Map) list.get(2)).get("F_PROBABILITY")); 返回的List中装载的是Map对象,没个map对象中的键值对应sql脚本的字段名字。
上一篇中介绍了JdbcTemplate 的一些常用的查询方法,在这篇里将介绍用JdbcTemplate做更新数据操作 和调用存储过程。
1. JdbcTemplate 更新数据 Java代码 public boolean switchStartOrderMeeting(String argument, String flag) throws DataAccessException { boolean result = false; String sql = sql = "update COMPETITION_BASE_T t set T.F_COMPETE_ORDER_STATE = '0'"+ " where T.F_COMPETITION_ID = ?"; int i = 0; //返回所影响的行数 i = this.getJdbcTemplate().update(sql, new Object[] { argument }); if (i > 0) { result = true; } return result; } public boolean switchStartOrderMeeting(String argument, String flag) throws DataAccessException { boolean result = false; String sql = sql = "update COMPETITION_BASE_T t set T.F_COMPETE_ORDER_STATE = '0'"+ " where T.F_COMPETITION_ID = ?"; int i = 0; //返回所影响的行数 i = this.getJdbcTemplate().update(sql, new Object[] { argument }); if (i > 0) { result = true; } return result; }
这个方法很简单,update方法只要传入sql脚本和脚本参数(Object数组类型)。
2. JdbcTemplate 调用存储过程 Java代码 /**代码片段*/ //5个输入参数,3个输出参数 String sql = "{call GENERATEORDERS_P(?,?,?,?,?,?,?,?)}"; final String company_id = result.get("F_COMPANY_ID").toString(); // 公司ID final String competition_id = result.get("F_COMPETITION_ID").toString(); // 比赛ID final String current_phase = result.get("F_CURRENT_PHASE").toString(); // 当前周期 final String market_scope_id = result.get("F_MARKET_SCOPE_ID").toString(); // 市场ID final String market_loca_id = result.get("F_MARKET_LOCA_ID").toString(); // 产品类型ID Map mapR = (Map) getJdbcTemplate().execute(sql,new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs)throws SQLException,DataAccessException { cs.setString(1, current_phase); // 输入参数 cs.setString(2, competition_id); // 输入参数 cs.setString(3, market_scope_id); // 输入参数 cs.setString(4, market_loca_id); // 输入参数 cs.setString(5, company_id); // 输入参数 cs.registerOutParameter(6,Types.VARCHAR);//输出参数 cs.registerOutParameter(7,Types.VARCHAR);//输出参数 cs.registerOutParameter(8,Types.VARCHAR);//输出参数 cs.execute(); Map map = new HashMap(); map.put("RESULT", cs.getString(6)); // 订单数量 map.put("F_PRICE", cs.getString(7)); // 订单价格 map.put("F_CPUT_MOD_ID", cs.getString(8));// 产品型号ID return map; } }); /**代码片段*/ //5个输入参数,3个输出参数 String sql = "{call GENERATEORDERS_P(?,?,?,?,?,?,?,?)}"; final String company_id = result.get("F_COMPANY_ID").toString(); // 公司ID final String competition_id = result.get("F_COMPETITION_ID").toString(); // 比赛ID final String current_phase = result.get("F_CURRENT_PHASE").toString(); // 当前周期 final String market_scope_id = result.get("F_MARKET_SCOPE_ID").toString(); // 市场ID final String market_loca_id = result.get("F_MARKET_LOCA_ID").toString(); // 产品类型ID Map mapR = (Map) getJdbcTemplate().execute(sql,new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs)throws SQLException,DataAccessException { cs.setString(1, current_phase); // 输入参数 cs.setString(2, competition_id); // 输入参数 cs.setString(3, market_scope_id); // 输入参数 cs.setString(4, market_loca_id); // 输入参数 cs.setString(5, company_id); // 输入参数 cs.registerOutParameter(6,Types.VARCHAR);//输出参数 cs.registerOutParameter(7,Types.VARCHAR);//输出参数 cs.registerOutParameter(8,Types.VARCHAR);//输出参数 cs.execute(); Map map = new HashMap(); map.put("RESULT", cs.getString(6)); // 订单数量 map.put("F_PRICE", cs.getString(7)); // 订单价格 map.put("F_CPUT_MOD_ID", cs.getString(8));// 产品型号ID return map; } });
execute方法返回的是一个Object对象,在这里我封装了一个Map对象返回。 |