/** * Updates a playlist that is selected by ID. * * @param p the playlist to be updated. * @throws SQLException */ public void update(Playlist p) throws SQLException { String sql = "" + "UPDATE Playlist " + "SET Name = ? " + "WHERE ID =" + p.getId(); Connection con = dataSource.getConnection(); PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, p.getName()); int affectedRows = ps.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Unable to update playlist."); } }
/** * Puts a new playlist-object on the server. * * @param p a given playlist-object. * @return the newly created playlist object. * @throws SQLException */ public Playlist insert(Playlist p) throws SQLException { String sql = "" + "INSERT INTO Playlist (Name)" + "VALUES(?)"; Connection con = dataSource.getConnection(); PreparedStatement ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); ps.setString(1, p.getName()); int affectedRows = ps.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Unable to insert playlist."); } ResultSet keys = ps.getGeneratedKeys(); keys.next(); int id = keys.getInt(1); return new Playlist(id, p); }