| 【IT168服务器学院】oracle+jsp中blob类型存储大文本问题解决方法
oracle存储大文本一直是一个棘手的问题。
一、存数据库:
<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*"errorPage=""%> <% //定义变量 java.sql.Connectionconn;//数据库连接对象 Stringsql; longid; ResultSetrs; Statementstmt,stmt1; java.sql.DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());//装载JDBC驱动程序 conn=java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.2:1521:lqxm","lqxm","lqxm");//连接数据库 request.setCharacterEncoding("GBK"); Stringtitle=request.getParameter("title"); Stringcontent=request.getParameter("content"); Stringsort=request.getParameter("sort"); Stringtype=request.getParameter("type"); Stringrq=request.getParameter("rq"); Stringqy=request.getParameter("qy"); //插入数据,此时blob字段中插入的是空值 sql="insertintot_flfg(xlh,title,content,rq,sort,type,qy)"; sql=sql+"Values(FLFG_SEQ.NEXTVAL,''"+title+"'',empty_clob(),''"+rq+"'',''"+sort+"'',''"+type+"'',''"+qy+"'')"; stmt=conn.createStatement(); stmt.executeUpdate(sql); conn.commit(); conn.setAutoCommit(false); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //取得刚才插入的ID sql="selectmax(xlh)asxlhfromt_flfg"; rs=stmt.executeQuery(sql); if(rs.next()){ id=rs.getInt("xlh"); } rs.close(); sql="selectcontentfromt_flfgwherexlh="+id+"forupdate"; rs=stmt.executeQuery(sql); if(rs.next()){ oracle.sql.CLOBclob=(oracle.sql.CLOB)rs.getClob(1); clob.putString(1,content); sql="updatet_flfgsetcontent=?wherexlh="+id+"";//将大文本更新进去,呵呵 PreparedStatementpstmt=conn.prepareStatement(sql); pstmt.setClob(1,clob); pstmt.executeUpdate(); } conn.commit(); stmt.close(); conn.close(); %> 二、检索显示数据:
<% ResultSetrs=flfgSave.searchOneInfo(request.getParameter("xlh"));//查询数据库获取记录集 rs.next(); inty; Stringcontent=""; oracle.sql.CLOBclob1; charac[]=newchar[299]; Stringtitle=rs.getString("title"); clob1=(oracle.sql.CLOB)rs.getObject("content"); Readerreader=clob1.getCharacterStream(); while((y=reader.read(ac,299))!=-1) content+=newString(ac,y);//这就是取出来的大文本 %> |