java读取oracle大文本

来源:互联网 发布:最火的网络歌曲2016 编辑:程序博客网 时间:2024/06/06 05:51

public class getColb{

public void getclob(){

try{
    DataSource dao=(DataSource)ComponentFactory.getBean("dataSource");
    Connection con=dao.getConnection();
    con.setAutoCommit(false);
    PreparedStatement ps=con.prepareStatement("update yc_news set news_title=?,news_content=?,news_secondtitle=?,news_summary=?,news_author=?,news_source=?," +
      "news_annex=?,news_time=?,news_open=?,news_keywords=?,news_approve=?,istop=?,browsenum=?,news_comment=?,news_refuse=?,news_auditor=? where news_id=?");
    ps.setString(1,news.getNewstitle());
    ps.setClob(2,CLOB.empty_lob());
    ps.setString(3,news.getNewssecondtitle());
    ps.setString(4,news.getNewssummary());
    ps.setString(5,news.getNewsauthor());
    ps.setString(6,news.getNewssource());
    ps.setString(7,news.getNewsannex());
    if(news.getNewstime()==null||"".equals(news.getNewstime())) {
     Timestamp ctim=new Timestamp((new java.util.Date()).getTime());
     news.setNewstime(ctim);
    }
    ps.setTimestamp(8,news.getNewstime());
    ps.setString(9,news.getNewsopen());
    ps.setString(10,news.getNewskeywords());
    ps.setString(11,news.getNewsapprove());
    ps.setInt(12,news.getIstop());
    ps.setInt(13,news.getBrowsenum());
    ps.setString(14,news.getNewscomment());
    ps.setString(15,news.getNewsrefuse());
    ps.setString(16,news.getNewsauditor());
    ps.setInt(17,news.getNewsid());
    ps.executeUpdate();
    ps.close();
    ps=con.prepareStatement("select news_content from yc_news where news_id=? for update");
    ps.setInt(1,news.getNewsid());
    ResultSet rs=ps.executeQuery();
    if(rs.next()){
     LobPros.modifyClob(rs,1,news.getNewscontent());
    }
    rs.close();
    ps.close();
    con.commit();
    con.close();
   }
   catch(Exception e){
    e.printStackTrace();
   }

}

}

public class LobPros {
 public static String readClob(ResultSet rs,String fname)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   if(fname==null||fname.length()==0)throw new SQLException("fname couldn't be null");
   oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob(fname);
   BufferedReader in=new BufferedReader(clob.getCharacterStream());
   StringBuffer sbRet=new StringBuffer(4096);
   int c;
   while((c=in.read())!=-1){
    sbRet.append((char)c);
   }
   in.close();
   return sbRet.toString();
  }
  catch(Exception e){
   e.printStackTrace();
   return null;
  }
 }
 public static String readClob(ResultSet rs,int i)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob(i);
   BufferedReader in=new BufferedReader(clob.getCharacterStream());
   StringBuffer sbRet=new StringBuffer(4096);
   int c;
   while((c=in.read())!=-1){
    sbRet.append((char)c);
   }
   in.close();
   return sbRet.toString();
  }
  catch(Exception e){
   e.printStackTrace();
   return null;
  }
 }
 public static void insertClob(ResultSet rs,String fname,String sValue)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   if(fname==null||fname.length()==0)throw new SQLException("fname couldn't be null");
   CLOB clob=(CLOB)rs.getClob(fname);
   BufferedWriter out=new BufferedWriter(clob.getCharacterOutputStream());
   BufferedReader in=new BufferedReader(new StringReader(sValue));
   int c;
   while((c=in.read())!=-1){
    out.write(c);
   }
   in.close();
   out.close();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void insertClob(ResultSet rs,int i,String sValue)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   CLOB clob=(CLOB)rs.getClob(i);
   java.io.Writer pw=clob.getCharacterOutputStream();
   pw.write(sValue);
   pw.flush();
   pw.close();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void modifyClob(ResultSet rs,String fname,String sValue)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   if(fname==null||fname.length()==0)throw new SQLException("fname couldn't be null");
   CLOB clob=(CLOB)rs.getClob(fname);
   BufferedWriter out=new BufferedWriter(clob.getCharacterOutputStream());
   BufferedReader in=new BufferedReader(new StringReader(sValue));
   int c;
   while((c=in.read())!=-1){
    out.write(c);
   }
   in.close();
   out.close();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void modifyClob(ResultSet rs,int i,String sValue)throws SQLException{
  try{
   if(rs==null) throw new SQLException("ResultSet Don't Be Ready!");
   CLOB clob=(CLOB)rs.getClob(i);
   java.io.Writer pw=clob.getCharacterOutputStream();
   pw.write(sValue);
   pw.flush();
   pw.close();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

 
原创粉丝点击