Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Huang committed Jul 17, 2024
1 parent 81c4a7f commit 1475b3c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 38 deletions.
83 changes: 65 additions & 18 deletions src/javalin/adding-a-model-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,78 @@ We'll need to create the "Data Access Layer". That is, the part of our applicati
Models are classes that we use to store data within the application.

```java
package com.corndel.bleeter.Models;
package com.corndel.bleeter.models;

public class User {

private Integer id;
public String username;
public boolean verified;

public User(Integer id, String username, boolean verified) {
this.id = id;
this.username = username;
this.verified = verified;
}

public Integer getId() {
return id;
}
}
```

Repositories are classes that interact with the database to let us persist, modify, and delete this data.


```java
package com.corndel.bleeter.Repositories;
package com.corndel.bleeter.repositories;

import com.corndel.bleeter.Models.User;
import com.corndel.bleeter.models.User;
import com.corndel.bleeter.DB;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserRepository {

public static List<User> findAll() throws SQLException {
var query = "SELECT id, username, verified FROM users";

try (var connection = DB.getConnection();
var statement = connection.createStatement();
var resultSet = statement.executeQuery(query);) {

var users = new ArrayList<User>();
while (resultSet.next()) {
var id = resultSet.getInt("id");
var username = resultSet.getString("username");
var verified = resultSet.getBoolean("verified");
users.add(new User(id, username, verified));
}
return users;
}
}
}
```

## Querying with substitutions

JDBC lets us set up _Prepared Statements_. These let us substitute in parameters to our SQL queries.

```js
static User findById(id) {
var query = `SELECT * FROM users WHERE id = ?`
const results = await db.raw(query, [id])
return results[0]
```java
public static User findById(id) {
var query = "SELECT id, username, verified FROM users WHERE id = ?"; // [!code highlight:7]
try (var connection = DB.getConnection();
var statement = connection.prepareStatement(query)) {
statement.setInt(1, id)
try (var resultSet = statement.executeQuery()) {
if (!resultSet.next()) {
return null;
}
var id = resultSet.getInt("id");
var username = resultSet.getString("username");
var verified = resultSet.getBoolean("verified");
return new User(id, username, verified);
}
}
}
```

Expand All @@ -46,8 +89,8 @@ you up to SQL injection attacks.

Consider

```js
User.findById('3; DROP TABLE users;')
```java
User.findById("3; DROP TABLE users;");
```

Always use prepared statements!
Expand All @@ -59,11 +102,15 @@ Always use prepared statements!
We can use an `INSERT` query with several parameters by putting more `?` and
passing the substitutions in the array:

```js
static async create(username, verified) {
const query =
'INSERT INTO users (username, verified) VALUES (?, ?) RETURNING *'
const results = await db.raw(query, [username, verified])
return results[0]
```java
public static User create(username, verified) {
var query = "INSERT INTO users (username, verified) VALUES (?, ?) RETURNING *";

}
```

::: info

Note the `RETURNING *`

:::
33 changes: 13 additions & 20 deletions src/javalin/connecting-to-a-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ This lets JDBC connect to our sqlite database.
We can then make queries to the database like so:

```java
package com.corndel.bleeter.Repositories;
package com.corndel.bleeter.repositories;

import com.corndel.bleeter.Models.User;
import com.corndel.bleeter.models.User;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserRepository {
public static List<User> findAll() throws SQLException {
var dbUrl = "jdbc:sqlite:bleeter.db"; // [!code focus:7]
var query =
"SELECT id, username, firstName, lastName, email, avatar FROM users";
var dbUrl = "jdbc:sqlite:bleeter.db"; // [!code focus:6]
var query = "SELECT id, username, verified FROM users";

try (var connection = DriverManager.getConnection(dbUrl); // [!code highlight:3]
var statement = connection.createStatement();
Expand All @@ -48,11 +47,8 @@ public class UserRepository {
while (resultSet.next()) {
var id = resultSet.getInt("id");
var username = resultSet.getString("username");
var firstName = resultSet.getString("firstName");
var lastName = resultSet.getString("lastName");
var email = resultSet.getString("email");
var avatar = resultSet.getString("avatar");
users.add(new User(id, username, firstName, lastName, email, avatar));
var verified = resultSet.getBoolean("verified");
users.add(new User(id, username, verified));
}
return users;
}
Expand Down Expand Up @@ -96,9 +92,9 @@ Now we can make a connection through this class instead of having to enter the c
For example, we've made a tiny change to the code above:

```java
package com.corndel.bleeter.Repositories;
package com.corndel.bleeter.repositories;

import com.corndel.bleeter.Models.User;
import com.corndel.bleeter.models.User;
import com.corndel.bleeter.DB; // [!code ++]
import java.sql.DriverManager; // [!code --]
import java.sql.SQLException;
Expand All @@ -107,23 +103,20 @@ import java.util.List;

public class UserRepository {
public static List<User> findAll() throws SQLException {
var dbUrl = "jdbc:sqlite:bleeter.db"; // [!code --] // [!code focus:8]
var query =
"SELECT id, username, firstName, lastName, email, avatar FROM users";
var dbUrl = "jdbc:sqlite:bleeter.db"; // [!code --] // [!code focus:7]
var query = "SELECT id, username, verified FROM users";

try (var connection = DriverManager.getConnection(dbUrl); // [!code --]
try (var connection = DB.getConnection(); // [!code ++]
var statement = connection.createStatement();
var resultSet = statement.executeQuery(query);) {

var users = new ArrayList<User>();
while (resultSet.next()) {
var id = resultSet.getInt("id");
var username = resultSet.getString("username");
var firstName = resultSet.getString("firstName");
var lastName = resultSet.getString("lastName");
var email = resultSet.getString("email");
var avatar = resultSet.getString("avatar");
users.add(new User(id, username, firstName, lastName, email, avatar));
var verified = resultSet.getBoolean("verified");
users.add(new User(id, username, verified));
}
return users;
}
Expand Down

0 comments on commit 1475b3c

Please sign in to comment.