Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266009
  • 博文数量: 34
  • 博客积分: 569
  • 博客等级: 中士
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-10 14:10
文章分类

全部博文(34)

文章存档

2016年(6)

2014年(1)

2013年(5)

2012年(22)

我的朋友

分类: Java

2013-03-15 17:19:59

背景

现有项目需要在后端增加公告的管理,实现CRUD功能,公告存储在数据库中使用clob字段。因为oracle的限制,sql语句的字符数不能超过4000个,所以不论是直接操作sql还是存储过程都会出现异常。

解决方法

按照公司的编码风格,除了查询,其他操作都必须使用存储过程调用。


  1. PROCEDURE add(v_title IN VARCHAR2,
  2.               v_content IN CLOB,
  3.               v_classify IN CHAR,
  4.               v_contact IN VARCHAR2,
  5.               v_memo IN VARCHAR2,
  6.               v_opentime IN VARCHAR2,
  7.               v_mobile IN VARCHAR2,
  8.               v_mail IN VARCHAR2,
  9.               v_scope IN CHAR,
  10.               v_type IN CHAR,
  11.               v_area IN VARCHAR2,
  12.               v_oper IN VARCHAR2,
  13.               v_ret OUT NUMBER,
  14.               v_desc OUT VARCHAR2);


因为使用10g的数据库,可以在存储过程内部把clob字段当作普通字段,数据库会自动处理,所以这里不写过程体。

 


  1. @Override
  2.     public Map<String, Object> addCallBid(final Map<String, Object> map) {
  3.         final Map<String, Object> result = new HashMap<String, Object>();
  4. //         PROCEDURE add(v_title IN VARCHAR2,
  5. //     v_content IN CLOB,
  6. //     v_classify IN CHAR,
  7. //     v_contact IN VARCHAR2,
  8. //     v_memo IN VARCHAR2,
  9. //     v_opentime IN VARCHAR2,
  10. //     v_mobile IN VARCHAR2,
  11. //     v_mail IN VARCHAR2,
  12. //     v_scope IN CHAR,
  13. //     v_type IN CHAR,
  14. //     v_area IN VARCHAR2,
  15. //     v_oper IN VARCHAR2,
  16. //     v_ret OUT NUMBER,
  17. //     v_desc OUT VARCHAR2)
  18.         String callString = "{call pkg_operannouncement.add(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?)}";
  19.         getJdbcTemplate().execute(callString, new CallableStatementCallback<Object>() {

  20.             @Override
  21.             public Object doInCallableStatement(CallableStatement cs)
  22.                     throws SQLException, DataAccessException {
  23.                 String title = map.containsKey("title")?map.get("title").toString():"";
  24.                 String content = map.containsKey("content")?map.get("content").toString():"";
  25.                 String classify = map.containsKey("classify")?map.get("classify").toString():"";
  26.                 String contact = map.containsKey("contact")?map.get("contact").toString():"";
  27.                 String memo = map.containsKey("memo")?map.get("memo").toString():"";
  28.                 String opentime = map.containsKey("opentime")?map.get("opentime").toString():"";
  29.                 String mobile = map.containsKey("mobile")?map.get("mobile").toString():"";
  30.                 String mail = map.containsKey("mail")?map.get("mail").toString():"";
  31.                 String scope = map.containsKey("scope")?map.get("scope").toString():"";
  32.                 String type = map.containsKey("type")?map.get("type").toString():"";
  33.                 String area = map.containsKey("area")?map.get("area").toString():"";
  34.                 String oper = map.containsKey("oper")?map.get("oper").toString():"";
  35.                 
  36.                 cs.setString(1, title);
  37.                 Reader reader = new StringReader(content);
  38.                 cs.setCharacterStream(2, reader, content.length());
  39.                 cs.setString(3, classify);
  40.                 cs.setString(4, contact);
  41.                 cs.setString(5, memo);
  42.                 cs.setString(6, opentime);
  43.                 cs.setString(7, mobile);
  44.                 cs.setString(8, mail);
  45.                 cs.setString(9, scope);
  46.                 cs.setString(10, type);
  47.                 cs.setString(11, area);
  48.                 cs.setString(12, oper);
  49.                 cs.registerOutParameter(13, Types.NUMERIC);
  50.                 cs.registerOutParameter(14, Types.VARCHAR);
  51.                 
    1. Reader reader = new StringReader(content);

    2. cs.setCharacterStream(2, reader, content.length());


  52.                 cs.execute();
  53.                 int ret = cs.getInt(13);
  54.                 String desc = cs.getString(14);
  55.                 result.put("ret", ret);
  56.                 result.put("desc", desc);
  57.                 return null;
  58.             }
  59.         });
  60.         return result;
  61.     }


 

主要是


  1. Reader reader = new StringReader(content);

  2. cs.setCharacterStream(2, reader, content.length());


阅读(1380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~