Skip to content

Commit 8bdafbb

Browse files
authored
Merge branch 'main' into js
2 parents e63408e + c262c12 commit 8bdafbb

31 files changed

+1565
-1
lines changed

.cbfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[languages]
2+
js = ["prettier"]
3+
java = ["google-java-format -"]

.vitepress/config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default defineConfig({
2222
'/bash/': sidebars.bash,
2323
'/express/': sidebars.express,
2424
'/git/': sidebars.git,
25+
'/javalin/': sidebars.javalin,
2526
'/html-css/': sidebars.htmlCss,
2627
'/java/': sidebars.java,
2728
'/js/': sidebars.javascript,

.vitepress/sidebars/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { bash } from './bash.js'
22
export { express } from './express.js'
33
export { git } from './git.js'
4+
export { javalin } from './javalin.js'
45
export { htmlCss } from './html-css.js'
56
export { javascript } from './javascript.js'
67
export { java } from './java.js'

.vitepress/sidebars/java.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,81 @@ export const java = [
1818
},
1919
{ text: 'Data types', link: '/java/data-types' }
2020
]
21+
},
22+
{
23+
text: 'Data and iteration',
24+
collapsed: true,
25+
items: [
26+
{
27+
text: 'Arrays',
28+
collapsed: true,
29+
items: [
30+
{ text: 'qq', link: '/java/qq' }
31+
]
32+
},
33+
{
34+
text: 'Strings',
35+
collapsed: true,
36+
items: [
37+
{ text: 'qq', link: '/java/qq' }
38+
]
39+
},
40+
{
41+
text: 'Loops',
42+
collapsed: true,
43+
items: [
44+
{ text: 'qq', link: '/java/qq' }
45+
]
46+
},
47+
48+
{
49+
text: 'Objects',
50+
collapsed: true,
51+
items: [
52+
{ text: 'qq', link: '/java/qq' }
53+
]
54+
}
55+
]
56+
},
57+
58+
{
59+
text: 'Object oriented programming',
60+
collapsed: true,
61+
items: [
62+
{ text: 'qq', link: '/java/qq' },
63+
{
64+
text: 'Design patterns',
65+
collapsed: true,
66+
items: [
67+
{ text: 'qq', link: '/java/qq' }
68+
]
69+
}
70+
]
71+
},
72+
73+
{
74+
text: 'Testing',
75+
collapsed: true,
76+
items: [
77+
{ text: 'qq', link: '/java/qq' }
78+
]
79+
},
80+
81+
{
82+
text: 'Building projects',
83+
collapsed: true,
84+
items: [
85+
{ text: 'Creating a Maven project', link: '/java/qq' },
86+
{ text: 'Making a CLI', link: '/java/qq' },
87+
{ text: 'Handling Errors', link: '/java/qq' },
88+
]
89+
},
90+
91+
{
92+
text: 'Asynchronous code',
93+
collapsed: true,
94+
items: [
95+
{ text: 'qq', link: '/java/qq' }
96+
]
2197
}
2298
]

.vitepress/sidebars/javalin.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export const javalin = [
2+
{ text: 'Introduction', link: '/javalin/index' },
3+
{
4+
text: 'Data layer',
5+
items: [
6+
{
7+
text: 'Managing the database',
8+
link: '/javalin/managing-the-database'
9+
},
10+
{
11+
text: 'Connecting to a database',
12+
link: '/javalin/connecting-to-a-database'
13+
},
14+
{
15+
text: 'Adding a model layer',
16+
link: '/javalin/adding-a-model-layer'
17+
}
18+
]
19+
},
20+
{
21+
text: 'Creating an API',
22+
items: [
23+
{ text: 'Creating a server', link: '/javalin/creating-a-server' },
24+
{ text: 'Routing and Controllers', link: '/javalin/routing' },
25+
{
26+
text: 'Request and response',
27+
link: '/javalin/request-response',
28+
collapsed: true,
29+
items: [
30+
{ text: 'Query params', link: '/javalin/query-params' },
31+
{ text: 'URL params', link: '/javalin/url-params' },
32+
{ text: 'Body and headers', link: '/javalin/body-and-headers' }
33+
]
34+
},
35+
{ text: 'Sending errors', link: '/javalin/sending-errors' },
36+
]
37+
},
38+
{
39+
text: 'Server side rendering',
40+
items: [
41+
{ text: 'Static files', link: '/javalin/static-files' },
42+
{ text: 'Views and templates', link: '/javalin/views-and-templates' },
43+
{ text: 'Using loops', link: '/javalin/using-loops' },
44+
{ text: 'Template partials', link: '/javalin/template-partials' },
45+
{ text: 'CSS and assets', link: '/javalin/css-and-assets' },
46+
{ text: 'User input', link: '/javalin/user-input' }
47+
]
48+
}
49+
]

.vitepress/sidebars/topnav.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ export const topnav = [
7474
link: '/dotnet/'
7575
}
7676
]
77+
},
78+
{
79+
text: 'Java',
80+
items: [
81+
{
82+
text: 'Javalin',
83+
link: '/javalin/'
84+
}
85+
]
7786
}
7887
]
7988
},

src/express/web-socket-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ server.on('connection', (socket) => {
4343
})
4444
```
4545

46-
When the `'connection'` even is emitted, we are passed a web socket, which we
46+
When the `'connection'` event is emitted, we are passed a web socket, which we
4747
can call whatever we want (here, we used `socket`).
4848

4949
## Listening for socket events

src/java/maven.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Creating a Maven project
2+
3+
##
4+
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
:::

src/javalin/adding-css.md

Whitespace-only changes.

0 commit comments

Comments
 (0)