轉自:http://hi.baidu.com/storex/blog/item/666b8f3889d7c92f97ddd89d.html Spring 框架 執行SQL getJdbcTemplate().query update 用法 2010-04-14 13:56
第二部分:轉自:http://peirenlei.javaeye.com/blog/353170ui Spring JdbcTemplate 應用 JdbcTemplate中的封裝了不少實用的方法。 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; } } 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; } 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; }
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(); 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"));
上一篇中介紹了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對象返回。 |