萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> oracle教程 >> oracle+jsp中blob類型存儲大文本問題解決方法

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_SENS99vIVE,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); //這就是取出來的大文本
%>
copyright © 萬盛學電腦網 all rights reserved