Skip to content

Instantly share code, notes, and snippets.

@dusanstanojeviccs
Created November 5, 2017 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dusanstanojeviccs/0723f6f142d0dee2f685d46ed08657b3 to your computer and use it in GitHub Desktop.
Save dusanstanojeviccs/0723f6f142d0dee2f685d46ed08657b3 to your computer and use it in GitHub Desktop.
public Optional<Book> findById(int id) throws SQLException {
PreparedStatement preparedStatement = database.getConnection().prepareStatement("select * from books where id = ?");
preparedStatement.setInt(1, id);
preparedStatement.execute();
ResultSet rs = preparedStatement.getResultSet();
if (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setDescription(rs.getString("description"));
return Optional.of(book);
}
return Optional.empty();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment