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