Skip to content

Commit 5a5de22

Browse files
feat(misc/databases): expand PreparedStatement section, add SQL tools/libraries (#691)
* Expand PreparedStatement section and add an entry about tools/libraries * Improve wording * Use sentence case * Fix typo on databases.md * Use appropriate examples * Refer to the latest jooq version instead of 3.20 * Remove examples and re-write. This time the description goes straight to the point
1 parent 90bf979 commit 5a5de22

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/content/docs/paper/dev/misc/databases.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,28 @@ Using prepared statements in Java with [`PreparedStatement`](jd:java:java.sql:ja
192192
helps prevent SQL injection. They separate SQL code from user input by using placeholders, reducing the risk of executing unintended SQL commands.
193193
**Always** use prepared statements to ensure the security and integrity of your data. Read more about SQL injection
194194
[here](https://www.baeldung.com/sql-injection).
195+
196+
When using `PreparedStatement` the `login` method will become:
197+
198+
```java
199+
public void login(DataSource dataSource, String username, String password) {
200+
try (Connection connection = dataSource.getConnection()) {
201+
PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
202+
statement.setString(1, username);
203+
statement.setString(2, password);
204+
ResultSet result = statement.executeQuery();
205+
// Do work
206+
} catch (Exception e) {
207+
// Handle any exceptions that arise from getting / handing the exception
208+
}
209+
}
210+
```
211+
212+
## Database tools
213+
214+
Given the complexity of working with databases (managing connections, building and securing queries, or just parsing the data) several tools
215+
exist in the world of Java to leverage this work.
216+
217+
Some plugin developers use lightweight tools like [JDBI](https://jdbi.org/), [JOOQ](https://www.jooq.org/doc/latest/manual/)
218+
or [Exposed](https://www.jetbrains.com/help/exposed/get-started-with-exposed.html), which take care of all the heavy lifting,
219+
allowing the developers to focus on their plugins rather than the database.

0 commit comments

Comments
 (0)