写入clob字段中

package myclass;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class in
       {
        int id=0;
        String cla;
        String title;
        int number;
        String date;
        String text;
        int count=1;
        String ly="";
        String objpath="";
        static String message="您的文章已经写入数据库,请等待管理员审查!";   
       
        public void in(){};  //构造函数

        private static String setStr(String getValue)  //检测字符串的合法性
                {
                 StringBuffer sbuff = new StringBuffer(getValue.length());
                 char getChar;
                
                 for (int cut=0; cut<getValue.length(); cut++)
                     {
                      getChar=getValue.charAt(cut);
                      
                      switch(getChar)
                             {
                              case '<': sbuff.append("&lt;");break;
                              case '>': sbuff.append("&gt;");break;
                              case '"': sbuff.append("&quot;");break;
                              case '\'':sbuff.append("&acute;");break;
                              case '&': sbuff.append("&amp;");break;
                              case 10 : sbuff.append("<br>");break;
                               default: sbuff.append(getChar);
                             }
                     }
                  return sbuff.toString();
                 }   
       
        public void setCla(String value)
               {
                cla=value;
               }
    
        public void setTitle(String value)
               {
                title=setStr(value);
               }
        public void setNumber(String value)
               {
                number=Integer.parseInt(value);
               }
        public void setDate(String value)
               {
                date=value;
               }
        public void setText(String value)
               {
                text=setStr(value);
               }
 
        public void exec()
               {
                ResultSet rSet;
                Statement ment;
                PreparedStatement pment;
                Connection conn;
                           
                try{
                    Context init = new InitialContext();
                    DataSource data=(DataSource)init.lookup("java:comp/env/jdbc/OracleDB");
                    conn=data.getConnection();
                    conn.setAutoCommit(false);
                    ment=conn.createStatement();

                   String sql="select MAX(ID) FROM 文档中心";
                   rSet=ment.executeQuery(sql);
                  
                   if(rSet.next() && rSet.getInt("MAX(ID)")>0)
                     id=rSet.getInt("MAX(ID)")+1;
                   else
                     id=1;

               
                   pment = conn.prepareStatement("insert into 文档中心(ID,归类号,标题,正文,IDS,发表日期,人气,领域,对象) values(?,?,?,empty_clob(),?,?,?,?,?)");
                   pment.setInt(1,id);
                   pment.setString(2,cla);
                   pment.setString(3,title);
                   pment.setInt(4,number);
                   pment.setString(5,date);
                   pment.setInt(6,count);
                   pment.setString(7,ly);
                   pment.setString(8,objpath);
                   pment.executeUpdate();

                   pment=conn.prepareStatement("select 正文 from 文档中心 where ID="+id+" for update");
                   rSet=pment.executeQuery();

                   if(rSet.next())
                     {
                       CLOB clob = (CLOB)rSet.getClob(1);
                           
                            Writer wr = clob.getCharacterOutputStream();
                                   wr.write(text);
                                   wr.flush();
                                   wr.close();
                     }
                    else
                       message="在写入文章内容时发生错误,请<a href=# onclick=history.back()>返回</a>";
                
                   rSet.close();
                   pment.close();
                   ment.close();
                   conn.commit();
                   conn.close();
                   }catch(IOException e1){
                         message="IO_错误:"+e1.toString();
                         }
                    catch(SQLException e2){
                         message="SQL_错误:"+e2.toString();
                         }
                   catch(Exception e3){
                         message="系统错误:"+e3.toString();
                         }
                 }
        public String cls()
               {
                return message;
               }
       }

posted on 2005-07-12 15:34  轻松逍遥子  阅读(403)  评论(0编辑  收藏  举报