关于oracle CLOB的操作

来源:互联网 发布:js indexof包含用法 编辑:程序博客网 时间:2024/06/05 06:00

oracle CLOB的处理:

insert:

Writer outStream = null;try {ApplicationContext ctx = null;ctx = new ClassPathXmlApplicationContext("applicationContext.xml");SqlSessionFactory sqlSessionFactory = (SqlSessionFactory )ctx.getBean("sqlSessionFactory");SqlSession sqlSession = sqlSessionFactory.openSession();Connection con = sqlSession.getConnection();con.setAutoCommit(false);  // 隐式提交Statement st = con.createStatement();String sqlguid = "select sys_guid() from dual";ResultSet rsguid = st.executeQuery(sqlguid);String sysguid = "";if(rsguid.next()){sysguid = rsguid.getString(1);}//插入一个空对象empty_clob()String sql = "insert into RES_KNOW(KNOW_UUID, KNOW_NICK, KNOW_AUTHOR, KNOW_DATE, KNOW_CT, KNOW_TYPE, KNOW_TITLE, KNOW_KEY, KNOW_CATE, KNOW_CONTENT)" +  "values('"+sysguid+"','"+resKnow.getKnowNick()+"','"+resKnow.getKnowAuthor()+"',SYSDATE,'"+resKnow.getKnowCt()+"','"+resKnow.getKnowType()+"','"+resKnow.getKnowTitle()+  "','"+resKnow.getKnowKey()+"','"+resKnow.getKnowCate()+"',empty_clob())";st.executeUpdate(sql);//锁定数据行进行更新,不用for update锁定不可以插入clobString sql2 = "select KNOW_CONTENT from RES_KNOW WHERE KNOW_UUID='"+sysguid+"' for update";ResultSet rs = st.executeQuery(sql2);if (rs.next()){//得到java.sql.Clob对象后强制转换为oracle.sql.CLOBoracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("KNOW_CONTENT");outStream = clob.getCharacterOutputStream();char[] c = resKnow.getKnowContent().toCharArray();outStream.write(c, 0, c.length);}outStream.flush();outStream.close();con.commit();con.close();ResponseJson(getJSON(getSuccess(true, "添加成功")));}

select:

try{List<ResKnow> KnowList = new ArrayList<ResKnow>();Reader inStream = null;// 获得数据库连接ApplicationContext ctx = null;ctx = new ClassPathXmlApplicationContext("applicationContext.xml");SqlSessionFactory sqlSessionFactory = (SqlSessionFactory )ctx.getBean("sqlSessionFactory");SqlSession sqlSession = sqlSessionFactory.openSession();Connection con = sqlSession.getConnection();con.setAutoCommit(false);  // 隐式提交Statement st = con.createStatement();String sql = "select * from RES_KNOW where KNOW_UUID='"+resKnow.getKnowUuid()+"'";ResultSet rs = st.executeQuery(sql);ResKnow know;while (rs.next()){know = new ResKnow();know.setKnowUuid(rs.getString("KNOW_UUID"));know.setKnowUnid(rs.getInt("KNOW_UNID"));know.setKnowName(rs.getString("KNOW_NAME"));know.setKnowAuthor(rs.getString("KNOW_AUTHOR"));know.setKnowType(rs.getString("KNOW_TYPE"));know.setKnowCt(rs.getString("KNOW_CT"));know.setKnowDate(rs.getString("KNOW_DATE"));know.setKnowNick(rs.getString("KNOW_NICK"));know.setKnowKey(rs.getString("KNOW_KEY"));know.setKnowTitle(rs.getString("KNOW_TITLE"));know.setKnowCate(rs.getString("KNOW_CATE"));java.sql.Clob clob = rs.getClob("KNOW_CONTENT");if(clob != null){inStream = clob.getCharacterStream();char[] c = new char[(int) clob.length()];inStream.read(c);know.setKnowContent(new String(c));inStream.close();}KnowList.add(know);}inStream.close();con.commit();con.close();}


原创粉丝点击