|
| 1 | +# Adding a model layer |
| 2 | + |
| 3 | +We'll need to create the _model_ layer and the _data access layer_. These are the parts of our application that deals with handling data models and persisiting data using the database. |
| 4 | + |
| 5 | +## Models and Repositories |
| 6 | + |
| 7 | +Models are classes that we use to represent data within the application. |
| 8 | + |
| 9 | +```java |
| 10 | +package com.corndel.bleeter.models; |
| 11 | + |
| 12 | +public class User { |
| 13 | + private Integer id; |
| 14 | + public String username; |
| 15 | + public boolean verified; |
| 16 | + |
| 17 | + public User(Integer id, String username, boolean verified) { |
| 18 | + this.id = id; |
| 19 | + this.username = username; |
| 20 | + this.verified = verified; |
| 21 | + } |
| 22 | + |
| 23 | + public Integer getId() { |
| 24 | + return id; |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Repositories are classes that interact with the database to let us persist, modify, and delete this data. |
| 30 | + |
| 31 | + |
| 32 | +```java |
| 33 | +package com.corndel.bleeter.repositories; |
| 34 | + |
| 35 | +import com.corndel.bleeter.models.User; |
| 36 | +import com.corndel.bleeter.DB; |
| 37 | +import java.sql.SQLException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +public class UserRepository { |
| 42 | + public static List<User> findAll() throws SQLException { |
| 43 | + var query = "SELECT id, username, verified FROM users"; |
| 44 | + |
| 45 | + try (var connection = DB.getConnection(); |
| 46 | + var statement = connection.createStatement(); |
| 47 | + var resultSet = statement.executeQuery(query);) { |
| 48 | + |
| 49 | + var users = new ArrayList<User>(); |
| 50 | + while (resultSet.next()) { |
| 51 | + var id = resultSet.getInt("id"); |
| 52 | + var username = resultSet.getString("username"); |
| 53 | + var verified = resultSet.getBoolean("verified"); |
| 54 | + users.add(new User(id, username, verified)); |
| 55 | + } |
| 56 | + return users; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Querying with substitutions |
| 63 | + |
| 64 | +JDBC lets us set up _Prepared Statements_. These let us substitute in parameters to our SQL queries. |
| 65 | + |
| 66 | +```java |
| 67 | +public static User findById(id) { |
| 68 | + var query = "SELECT id, username, verified FROM users WHERE id = ?"; // [!code highlight] |
| 69 | + try (var connection = DB.getConnection(); |
| 70 | + var statement = connection.prepareStatement(query)) { // [!code highlight] |
| 71 | + statement.setInt(1, id) // [!code highlight] |
| 72 | + try (var resultSet = statement.executeQuery()) { |
| 73 | + if (!resultSet.next()) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + var id = resultSet.getInt("id"); |
| 77 | + var username = resultSet.getString("username"); |
| 78 | + var verified = resultSet.getBoolean("verified"); |
| 79 | + return new User(id, username, verified); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +::: danger |
| 86 | + |
| 87 | +Do not be tempted to interpolate raw arguments into the query string. This opens |
| 88 | +you up to SQL injection attacks. |
| 89 | + |
| 90 | +Consider |
| 91 | + |
| 92 | +```java |
| 93 | + User.findById("3; DROP TABLE users;"); |
| 94 | +``` |
| 95 | + |
| 96 | +Always use prepared statements! |
| 97 | + |
| 98 | +::: |
| 99 | + |
| 100 | +## Inserting data |
| 101 | + |
| 102 | +We can use an `INSERT` query with several parameters by putting more `?` and |
| 103 | +passing the substitutions in with `.setString()`, `.setInt()`, or the appropriate set method for the datatype: |
| 104 | + |
| 105 | +```java |
| 106 | +public static User create(username, verified) { |
| 107 | + var query = // [!code highlight:2] |
| 108 | + "INSERT INTO users (username, verified) VALUES (?, ?) RETURNING *"; |
| 109 | + |
| 110 | + try (var connection = DB.getConnection(); |
| 111 | + var statement = con.prepareStatement(query)) { |
| 112 | + statement.setString(1, username); // [!code highlight] |
| 113 | + statement.executeUpdate(); // [!code highlight] |
| 114 | + |
| 115 | + try (var resultSet = statement.getResultSet()) { |
| 116 | + rs.next() |
| 117 | + var id = rs.getInt("id"); |
| 118 | + return new User(id, username, verified) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +::: info |
| 125 | + |
| 126 | +Note the `RETURNING *` causes the statement to have a `resultSet` after execution. This lets us get the `id` and other fields of the newly created `User` from the database. |
| 127 | + |
| 128 | +::: |
0 commit comments