@Override public ImagesPage findByIdWithoutData(int idImg) { ImagesPage image = null; String sql = "SELECT * FROM imagespages WHERE id=" + idImg; try { conn = myDataSource.getConnection(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); if (rs.next()) { image = new ImagesPage(); image.setId(rs.getInt(1)); image.setDirectory(rs.getString(5)); image.setFilename(rs.getString(4)); image.setIdPageText(rs.getInt(3)); } } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } finally { try { conn.close(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } } return image; }
@Override public void addImage(ImagesPage img) { String sql = "INSERT INTO imagespages VALUES (null,?,?,?,?)"; PreparedStatement pstat = null; try { conn = myDataSource.getConnection(); pstat = conn.prepareStatement(sql); pstat.setBytes(1, img.getImageData()); pstat.setInt(2, img.getIdPageText()); pstat.setString(3, img.getFilename()); pstat.setString(4, img.getDirectory()); pstat.executeUpdate(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } finally { try { if (pstat != null) { pstat.close(); } myDataSource.closeConnection(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } } }
@Override public List<ImagesPage> findAllWithoutData() { List<ImagesPage> imagesList = new ArrayList<>(); ImagesPage image = null; String sql = "SELECT * FROM imagespages"; Statement stat = null; ResultSet rs = null; try { conn = myDataSource.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery(sql); while (rs.next()) { image = new ImagesPage(); image.setId(rs.getInt(1)); image.setDirectory(rs.getString(5)); image.setFilename(rs.getString(4)); image.setIdPageText(rs.getInt(3)); imagesList.add(image); } } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } finally { try { stat.close(); rs.close(); conn.close(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } } return imagesList; }
@Override public void removeImage(ImagesPage img) { String sql = "DELETE FROM imagespages WHERE id=?"; PreparedStatement pstat = null; try { conn = myDataSource.getConnection(); pstat = conn.prepareStatement(sql); pstat.setInt(1, img.getId()); pstat.executeUpdate(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } finally { try { if (pstat != null) { pstat.close(); } myDataSource.closeConnection(); } catch (SQLException ex) { LOG.log(Level.SEVERE, null, ex); } } }