ContentDAOImpl.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:2k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.storage.jdbc;
  2. import net.javacoding.jspider.core.storage.spi.ContentDAOSPI;
  3. import net.javacoding.jspider.core.storage.spi.StorageSPI;
  4. import net.javacoding.jspider.core.logging.Log;
  5. import net.javacoding.jspider.core.logging.LogFactory;
  6. import java.io.InputStream;
  7. import java.io.ByteArrayInputStream;
  8. import java.sql.*;
  9. /**
  10.  * $Id: ContentDAOImpl.java,v 1.3 2003/04/11 16:37:05 vanrogu Exp $
  11.  */
  12. class ContentDAOImpl implements ContentDAOSPI {
  13.     protected Log log;
  14.     protected DBUtil dbUtil;
  15.     protected StorageSPI storage;
  16.     public ContentDAOImpl ( StorageSPI storage, DBUtil dbUtil ) {
  17.         this.log = LogFactory.getLog(ContentDAOSPI.class);
  18.         this.dbUtil = dbUtil;
  19.         this.storage = storage;
  20.     }
  21.     public void setBytes ( int id, byte[] bytes ) {
  22.         try {
  23.             Connection connection = dbUtil.getConnection();
  24.             PreparedStatement ps = connection.prepareStatement("insert into jspider_content ( id, content ) values ( ?, ? )");
  25.             ps.setInt(1, id);
  26.             ps.setBytes(2, bytes);
  27.             try {
  28.                 ps.execute();
  29.             } catch (IllegalArgumentException e) {
  30.                 log.error("IllegalArgumentException", e);
  31.             } finally {
  32.                 dbUtil.safeClose(ps, log);
  33.             }
  34.         } catch (SQLException e) {
  35.             log.error("SQLException", e);
  36.         }
  37.     }
  38.     public InputStream getInputStream(int id) {
  39.         byte[] bytes = null;
  40.         PreparedStatement ps = null;
  41.         ResultSet rs = null;
  42.         try {
  43.             Connection connection = dbUtil.getConnection();
  44.             ps = connection.prepareStatement("select content from jspider_content where id=?");
  45.             ps.setInt(1, id);
  46.             rs = ps.executeQuery();
  47.             if ( rs.next() ) {
  48.               bytes = rs.getBytes("content");
  49.             }
  50.         } catch (SQLException e) {
  51.             log.error("SQLException", e);
  52.         } finally {
  53.             dbUtil.safeClose(rs, log);
  54.             dbUtil.safeClose(ps, log);
  55.         }
  56.         return new ByteArrayInputStream ( bytes );
  57.     }
  58. }