Created
November 5, 2017 22:03
-
-
Save dusanstanojeviccs/0723f6f142d0dee2f685d46ed08657b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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