日期:2014-05-16  浏览次数:20494 次

将网页文本(HTML)保存到ORACLE数据库CLOB字

网上常见的例子总是将文本文件上传至数据库的方法。今天在做文档管理相关系统时,需要将网页上的文本输入框(textarea或input)中的内容,上传到ORACLE数据库的CLOB字段中去。在网上找了好长时间,总算有所收获,现将方法总结如下,其中部分代码为其它网友的源码:

  一、上传

private void updateContent(Connection conn, Information info) throws
Exception {
PreparedStatement pstmt = conn.prepareStatement(
"SELECT CONTENT FROM INFO_CONTENT WHERE ID=? FOR UPDATE");
pstmt.setInt(1, info.getId());
ResultSet rs = pstmt.executeQuery();
/* 取出此CLOB对象 */
if (rs.next()) {
//Weblogic这样写
OracleThinClob clob = (OracleThinClob) rs.getClob(1);
//其它服务器这样写
//oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob(1);
/* 向CLOB对象中写入数据 */
/*
//保存文件
//BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
//BufferedReader in = new BufferedReader(new FileReader(filename));
*/
//保存字符串
Writer out = clob.getCharacterOutputStream();
out.write(info.getContent());
out.flush();
out.close();
rs.close();
pstmt.close();
}
}

二、在jsp中显示



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/whirlsun/archive/2005/07/16/427013.aspx
public Information getInformation(int id) throws Exception {
Information base = new Information();
Connection conn = null;
try {
conn = DBConnect.GainDBConnect();
PreparedStatement ps = conn.prepareStatement(
"SELECT * FROM INFO_CONTENT WHERE ID=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {

java.sql.Clob clob = (java.sql.Clob) rs.getClob("CONTENT");
/* 以字符形式输出 */
Reader out = new BufferedReader(clob.getCharacterStream());
BufferedReader bfClob = new BufferedReader(out);
String strClob = bfClob.readLine();
StringBuffer sbResult = new StringBuffer();
while (strClob != null) {
sbResult.append(strClob);
strClob = bfClob.readLine();
}
base.setContent(sbResult.toString());
out.close();

}
rs.close();
ps.close();
}
catch (Exception ex) {
System.out.println(ex);
throw ex;
}
finally {
conn.close();
}
return base;
}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/whirlsun/archive/2005/07/16/427013.aspx