存clob为空的值_将网页文本(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中顯示
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;
}
轉載請注明來源網站:www.itxm.cn謝謝!
分享到:
總結
以上是生活随笔為你收集整理的存clob为空的值_将网页文本(HTML)保存到ORACLE数据库CLOB字详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个关于奋斗的个性签名!
- 下一篇: 樊登高效休息法读书心得_《读懂一本书:樊