oracle+jsp中blob类型存储大文本问题解决方法

类别:Java 点击:0 评论:0 推荐:

oracle+jsp中blob类型存储大文本问题解决方法
oracle 存储大文本一直是一个棘手的问题。
一、存数据库:
<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%
 //定义变量
 java.sql.Connection conn; //数据库连接对象
 String sql;
 long id;
 ResultSet rs;
 Statement stmt,stmt1;

 java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //装载JDBC驱动程序
 conn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.2:1521:lqxm","lqxm","lqxm"); //连接数据库

 request.setCharacterEncoding("GBK");
 String title   = request.getParameter("title");
 String content = request.getParameter("content");
 String sort    = request.getParameter("sort");
 String type    = request.getParameter("type");
 String rq      = request.getParameter("rq");
 String qy      = request.getParameter("qy");
 //插入数据,此时blob字段中插入的是空值
 sql="insert into t_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="select max(xlh) as xlh from t_flfg ";
 rs=stmt.executeQuery(sql);
 if(rs.next()) {
  id=rs.getInt("xlh");
 }
 rs.close();

 sql = "select content from t_flfg where xlh="+id+" for update";
 rs = stmt.executeQuery(sql);
 if (rs.next()) {
  oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
  clob.putString(1, content);
  sql = "update t_flfg set content=? where xlh=" + id + ""; //将大文本更新进去,呵呵
  PreparedStatement pstmt   = conn.prepareStatement(sql);
  pstmt.setClob(1, clob);
  pstmt.executeUpdate();
 }

 conn.commit();
 stmt.close();
 conn.close();
%>

二、检索显示数据:
<%
 ResultSet rs = flfgSave.searchOneInfo(request.getParameter("xlh")); //查询数据库获取记录集
 rs.next();

 int y;
 String content = "";
 oracle.sql.CLOB clob1;
 char ac[] = new char[299];

 String title = rs.getString("title");

 clob1 = (oracle.sql.CLOB)rs.getObject("content");
 Reader reader = clob1.getCharacterStream();
 while((y = reader.read(ac, 0, 299)) != -1)
  content += new String(ac, 0, y); //这就是取出来的大文本
%>

本文地址:http://com.8s8s.com/it/it14091.htm