|
| 1 | +package com.baeldung.statmentVsPreparedstatment; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.PreparedStatement; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +public class PreparedStatementPersonDao { |
| 12 | + |
| 13 | + private final Connection connection; |
| 14 | + |
| 15 | + public PreparedStatementPersonDao(Connection connection) { |
| 16 | + this.connection = connection; |
| 17 | + } |
| 18 | + |
| 19 | + public Optional<PersonEntity> getById(int id) throws SQLException { |
| 20 | + String query = "SELECT id, name FROM persons WHERE id = ?"; |
| 21 | + |
| 22 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 23 | + preparedStatement.setInt(1, id); |
| 24 | + ResultSet resultSet = preparedStatement.executeQuery(); |
| 25 | + |
| 26 | + if (resultSet.first()) { |
| 27 | + |
| 28 | + PersonEntity result = new PersonEntity(resultSet.getInt("id"), |
| 29 | + resultSet.getString("name")); |
| 30 | + |
| 31 | + return Optional.of(result); |
| 32 | + } else { |
| 33 | + return Optional.empty(); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public void insert(PersonEntity personEntity) throws SQLException { |
| 39 | + |
| 40 | + String query = "INSERT INTO persons(id, name) VALUES( ?, ?)"; |
| 41 | + |
| 42 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 43 | + preparedStatement.setInt(1, personEntity.getId()); |
| 44 | + preparedStatement.setString(2, personEntity.getName()); |
| 45 | + preparedStatement.executeUpdate(); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public void insert(List<PersonEntity> personEntities) throws SQLException { |
| 50 | + String query = "INSERT INTO persons(id, name) VALUES( ?, ?)"; |
| 51 | + |
| 52 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 53 | + for (PersonEntity personEntity : personEntities) { |
| 54 | + preparedStatement.setInt(1, personEntity.getId()); |
| 55 | + preparedStatement.setString(2, personEntity.getName()); |
| 56 | + preparedStatement.addBatch(); |
| 57 | + } |
| 58 | + preparedStatement.executeBatch(); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + public void update(PersonEntity personEntity) throws SQLException { |
| 63 | + String query = "UPDATE persons SET name = ? WHERE id = ?"; |
| 64 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 65 | + preparedStatement.setString(1, personEntity.getName()); |
| 66 | + preparedStatement.setInt(2, personEntity.getId()); |
| 67 | + preparedStatement.executeUpdate(); |
| 68 | + } |
| 69 | + |
| 70 | + public void deleteById(int id) throws SQLException { |
| 71 | + String query = "DELETE FROM persons WHERE id = ?"; |
| 72 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 73 | + preparedStatement.setInt(1, id); |
| 74 | + preparedStatement.executeUpdate(); |
| 75 | + } |
| 76 | + |
| 77 | + public List<PersonEntity> getAll() throws SQLException { |
| 78 | + String query = "SELECT id, name FROM persons"; |
| 79 | + |
| 80 | + PreparedStatement preparedStatement = connection.prepareStatement(query); |
| 81 | + ResultSet resultSet = preparedStatement.executeQuery(); |
| 82 | + List<PersonEntity> result = new ArrayList<>(); |
| 83 | + while (resultSet.next()) { |
| 84 | + result.add(new PersonEntity(resultSet.getInt("id"), resultSet.getString("name"))); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | +} |
0 commit comments