@Override public void create(Feed feed) { try { Connection cn = null; try { cn = JdbcConnection.getInstance().getConnection(); PreparedStatement st = null; try { st = cn.prepareStatement( "INSERT INTO feed (typeFeed, valueFeed) values (?, ?)", Statement.RETURN_GENERATED_KEYS); st.setString(1, feed.name()); st.setString(2, feed.getValueFeed()); st.executeUpdate(); } finally { if (st != null) st.close(); st = null; } } finally { if (cn != null) cn.close(); cn = null; } } catch (SQLException e) { e.printStackTrace(); } }
@Override public void update(int id, Feed feed) { try { Connection cn = null; try { JdbcConnection connection = JdbcConnection.getInstance(); cn = connection.getConnection(); PreparedStatement st = null; try { st = cn.prepareStatement("UPDATE feed SET (typeFeed = ?, valueFeed = ?) WHERE idFeed = ?"); st.setString(1, feed.name()); st.setString(2, feed.getValueFeed()); st.setInt(3, id); st.executeUpdate(); } finally { if (st != null) st.close(); st = null; } } finally { if (cn != null) cn.close(); cn = null; } } catch (SQLException e) { e.printStackTrace(); } }
@Override public Feed find(int id) { Feed feed = null; try { Connection cn = null; try { JdbcConnection connection = JdbcConnection.getInstance(); cn = connection.getConnection(); PreparedStatement st = null; try { st = cn.prepareStatement("SELECT * FROM feed WHERE idFeed = ?"); st.setInt(1, id); ResultSet rs = null; try { rs = st.executeQuery(); if (rs.next()) { feed = Feed.valueOf(rs.getString(2)); } } finally { if (rs != null) rs.close(); rs = null; } } finally { if (st != null) st.close(); st = null; } } finally { if (cn != null) cn.close(); cn = null; } } catch (SQLException e) { e.printStackTrace(); } return feed; }
@Override public List<Feed> findAll() { List<Feed> feeds = new ArrayList<Feed>(); try { Connection cn = null; try { cn = JdbcConnection.getInstance().getConnection(); Statement st = null; try { st = cn.createStatement(); ResultSet rs = null; try { rs = st.executeQuery("SELECT * FROM feed "); while (rs.next()) { feeds.add(Feed.valueOf(rs.getString(2))); } } finally { if (rs != null) rs.close(); rs = null; } } finally { if (st != null) st.close(); st = null; } } finally { if (cn != null) cn.close(); cn = null; } } catch (SQLException e) { e.printStackTrace(); } return feeds; }