将网页文本(HTML)保存到ORACLE数据库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;

}

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

转载请注明来源网站:blog.ytso.com谢谢!

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/database/4534.html

(0)
上一篇 2021年7月16日 21:59
下一篇 2021年7月16日 21:59

相关推荐

发表回复

登录后才能评论