NCLOB插入更新
? ? ? 搞了半天终于弄懂了oracle中NCLob的操作。其实它就是一个指针,我们插入修改事实上要改的是指针指向的地址数据。
也就是为什么,要先取出所指向的地址数据才能修改了。不过在操作Clob字段时,我的问题主要是String太长,传不到数据库。有人说换驱动包,但是我太固执没有换。后来试出来,可以分段传进存储过程就能搞定了。
?
首先content字段就是NCLOB类型的。存储过程如下:
--添加公告procedure addNotice( m_title base_notice_data.title%type,--标题 m_docnum base_notice_data.docnum%type,-- m_author base_notice_data.author%type,--作者 m_content base_notice_data.content%type,--内容 m_id out base_notice_data.id%type--返回id)isbegin insert into base_notice_data values( BASE_NOTICE_DATA_SEQ.nextval, m_title, m_docnum, m_author, 0, 0, m_content, sysdate, 0,1, BASE_NOTICE_DATA_SEQ.currval ); select BASE_NOTICE_DATA_SEQ.currval into m_id from dual;end addNotice;
-- 根据id查 只查content用于更新clobprocedure updateNoticeContent( m_id base_notice_data.id%type,--更新的id m_offset number,--指定开始操作的偏移量 m_content base_notice_data.content%type--更新的内容) aslobloc NCLOB;begin SELECT content INTO lobloc from base_notice_data where id =m_id FOR UPDATE; dbms_lob.write(lobloc,length(m_content),m_offset,m_content);end updateNoticeContent;
?java操作类中的添加公告方法:
public int addNotice(NoticeBean mb) throws Exception {String sql = "{call PKG_Notice.addNotice(?,?,?,?,?)}";String sql2= "{call PKG_Notice.updateNoticeContent(?,?,?)}";int id = 0;try {conn = DatabaseFactory.getConnection();sta = conn.prepareCall(sql);int i = 1;int length = mb.getContent().length();//内容长度String content = "";if(length>=2500){content = mb.getContent().substring(0, 2499);}else{content = mb.getContent();}sta.setString(i++, mb.getTitle());sta.setString(i++, mb.getDocnum());sta.setString(i++, mb.getAuthor());sta.setString(i++, content);sta.registerOutParameter(i++, OracleTypes.NUMBER);sta.execute();id = sta.getInt(i-1);//更新nclob字段if(length>2500){int times = length/2500;for(int j=0;j<times;j++){if(length>2500*j+4999){content = mb.getContent().substring(2500*j+2499, 2500*j+4999);}else{content = mb.getContent().substring(2500*j+2499, length);}sta = conn.prepareCall(sql2);sta.setLong(1, id);sta.setLong(2, 2500*j+2499);//插入的开始偏移量sta.setString(3, content);sta.execute();}}} catch (Exception e) {e.printStackTrace();} finally {close();}return id;}?